未处理的异常:类型“bool”不是类型转换中“String”类型的子类型
Posted
技术标签:
【中文标题】未处理的异常:类型“bool”不是类型转换中“String”类型的子类型【英文标题】:Unhandled Exception: type 'bool' is not a subtype of type 'String' in type cast 【发布时间】:2021-10-19 17:18:41 【问题描述】:这是我的完整代码。我没有制作模型类,因为我没有从服务器获取响应。
import 'dart:convert';
import 'package:attendance_worklog/AttendanceLog/attendanceLogModel.dart';
import 'package:attendance_worklog/AttendanceLog/attendanceLogService.dart';
import 'package:attendance_worklog/login/loginModel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import 'package:http/http.dart' as http;
class AttendanceLog extends StatefulWidget
LoginModel? data;
AttendanceLog(Key? key, required this.data) : super(key: key);
@override
_AttendanceLogState createState() => _AttendanceLogState();
class _AttendanceLogState extends State<AttendanceLog>
List<AttendanceLogModel> _attendanceLogModel = [];
List<String> day = ['day', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
@override
void initState()
// TODO: implement initState
super.initState();
AttendanceLogService _attendanceLogService =
AttendanceLogService(data: widget.data);
_attendanceLogService.getLog().then((value)
setState(()
_attendanceLogModel = value;
);
);
@override
Widget build(BuildContext context)
int flag = 0;
int per()
for (var i = 0; i < _attendanceLogModel.length; i++)
if (_attendanceLogModel[i].attendance == true)
flag = flag + 1;
return flag;
int p = per();
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop(),
color: Color(0xff979797),
iconSize: 30,
),
title: Text(
'Attendance Log',
style: TextStyle(
color: Color(0xffA468AB),
fontWeight: FontWeight.w700,
fontFamily: 'Merriweather',
fontSize: 32),
),
actions: [
IconButton(
onPressed: (),
icon: Icon(Icons.check_outlined))
],
),
body: ListView(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 25),
child: Column(
children: [
LinearPercentIndicator(
linearStrokeCap: LinearStrokeCap.roundAll,
lineHeight: 18.0,
percent: p / _attendanceLogModel.length,
backgroundColor: Color(0xffFF0000),
progressColor: Color(0xff14FF00),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'$(p / _attendanceLogModel.length) * 100',
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 25,
fontFamily: 'Merriweather'),
),
Text(
'$100 - ((p / _attendanceLogModel.length) * 100)',
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 25,
fontFamily: 'Merriweather'),
)
],
)
],
),
),
Container(
height: 1,
width: double.maxFinite,
color: Color(0xffD3D3D3),
),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount:
null == _attendanceLogModel ? 0 : _attendanceLogModel.length,
itemBuilder: (context, index)
AttendanceLogModel attendanceLogModel =
_attendanceLogModel[index];
void toggleSwitch(bool value)
String hom = '';
if (attendanceLogModel.attendance == false)
setState(()
attendanceLogModel.attendance = true;
);
bool att = attendanceLogModel.attendance;
String id = attendanceLogModel.id;
PutAttendance send = new PutAttendance(data: widget.data);
var response = send.sendAttendance(att, id, hom);
else
setState(()
attendanceLogModel.attendance = false;
);
PutAttendance send = new PutAttendance(data: widget.data);
var response = send.sendAttendance(attendanceLogModel.attendance, attendanceLogModel.id, hom);
return Padding(
padding: EdgeInsets.all(10.0),
child: ListTile(
title: Text(
'Meet: $attendanceLogModel.name',
style: TextStyle(
fontSize: 17,
fontFamily: 'Merriweather',
fontWeight: FontWeight.w900),
),
subtitle: Text(
'$day[attendanceLogModel.start.weekday], $attendanceLogModel.start.day/$attendanceLogModel.start.month/$attendanceLogModel.start.year\n$attendanceLogModel.start.hour:$attendanceLogModel.start.minute to $attendanceLogModel.end.hour:$attendanceLogModel.end.minute',
style: TextStyle(
fontSize: 16,
fontFamily: 'Merriweather',
color: Color(0xff000000),
fontWeight: FontWeight.w100),
),
trailing: Transform.scale(
scale: 0.8,
child: CupertinoSwitch(
activeColor: Color(0xff14FF00),
value: attendanceLogModel.attendance,
onChanged: toggleSwitch,
trackColor: Color(0xffFF0000),
),
),
tileColor: Color(0xffF2F2F2),
),
);
),
],
),
);
class PutAttendance
LoginModel? data;
PutAttendance(required this.data);
var url = Uri.parse('http://10.0.2.2:5001/naf-vip-server/us-central1/api/attendance');
Future<http.Response> sendAttendance(bool attend, String id, String hom)
return http.put(url, headers:
'Authorization': 'Bearer $data!.token'
, body:
"attendance": attend,
"meetId": id,
"hom": hom
);
运行后显示异常为Unhandled Exception: type 'bool' is not a subtype of type 'String' in type cast
。
此外,它不会更新服务器中的值。我无法弄清楚问题是什么以及在哪里。请帮我解决这个问题。
这里是异常堆栈:
E/flutter (12852): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'bool' is not a subtype of type 'String' in type cast
E/flutter (12852): #0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
E/flutter (12852): #1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
E/flutter (12852): #2 CastMap.forEach (dart:_internal/cast.dart:287:13)
E/flutter (12852): #3 mapToQuery (package:http/src/utils.dart:17:7)
E/flutter (12852): #4 Request.bodyFields= (package:http/src/request.dart:137:12)
E/flutter (12852): #5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:87:17)
E/flutter (12852): #6 BaseClient.put (package:http/src/base_client.dart:37:7)
E/flutter (12852): #7 put.<anonymous closure> (package:http/http.dart:92:16)
E/flutter (12852): #8 _withClient (package:http/http.dart:164:20)
E/flutter (12852): #9 put (package:http/http.dart:91:5)
E/flutter (12852): #10 PutAttendance.sendAttendance (package:attendance_worklog/AttendanceLog/attendanceLog.dart:429:12)
E/flutter (12852): #11 _AttendanceLogState.build.<anonymous closure>.toggleSwitch (package:attendance_worklog/AttendanceLog/attendanceLog.dart:136:41)
E/flutter (12852): #12 _CupertinoSwitchState._handleTap (package:flutter/src/cupertino/switch.dart:226:24)
E/flutter (12852): #13 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (12852): #14 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
E/flutter (12852): #15 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter (12852): #16 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
E/flutter (12852): #17 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
E/flutter (12852): #18 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
E/flutter (12852): #19 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
E/flutter (12852): #20 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:287:11)
E/flutter (12852): #21 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
E/flutter (12852): #22 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
E/flutter (12852): #23 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
E/flutter (12852): #24 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
E/flutter (12852): #25 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (12852): #26 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (12852): #27 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (12852): #28 _invoke1 (dart:ui/hooks.dart:182:10)
E/flutter (12852): #29 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter (12852): #30 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (12852):
【问题讨论】:
可以添加 AttendanceLogModel 类吗? 【参考方案1】:http.put
的文档指出:
body
设置请求的正文。它可以是String
、List<int>
或Map<String, String>
。
您的sendAttendance
方法调用http.put
并使用bool
(attend
) 作为预期为Map<String, String>
的值。首先通过调用.toString()
将attend
转换为String
。
【讨论】:
以上是关于未处理的异常:类型“bool”不是类型转换中“String”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
未处理的异常:类型“int”不是类型转换中“List<int>”类型的子类型
Flutter 未处理的异常:类型“Null”不是类型转换中“List<dynamic>”类型的子类型
未处理的异常:类型“List<Widget>”不是类型转换中“Iterable<MyWidget>”类型的子类型
未处理的异常:类型“ImmutableMap<String, dynamic>”不是类型转换中“List<dynamic>”类型的子类型
Flutter [错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:类型“int”不是类型转换中“String”类型的子类型