Flutter/Dart - 未处理的异常:类型 'String' 不是 'index' 类型 'int' 的子类型
Flutter/Dart - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
我是 Flutter 的初学者,目前正在学习构建任务规划器的教程 (https://www.youtube.com/watch?v=xJSestoFlJ8&list=PLF9bFmu3NqWqchwS3DO9MDSl15IiHQWcr&index=9)
手动重写代码(数据库等)后,当我重新启动应用程序以在调试控制台中显示我保存到数据库中的项目时,我在使用 initstate 和 setstate 时收到错误提示
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
这是与错误相关的代码
final TextEditingController _textEditingController = TextEditingController();
var db = DatabaseHelper();
@override
void initState() {
super.initState();
_readTaskList();
}
void _handleSubmitted(String text) async {
_textEditingController.clear();
Tasks tasksItem = Tasks(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(tasksItem);
print("Item saved ID: $savedItemId");
}
_readTaskList() async {
List items = await db.getItems();
items.forEach((item) {
Tasks tasksItem = Tasks.map(items);
print("DB Items: ${tasksItem.itemName}");
});
}
这是完整的调试控制台日志
E/flutter ( 8249): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
E/flutter ( 8249): #0 new Tasks.map
package:gatorblocks_rbt/models/tasks.dart:12
E/flutter ( 8249): #1 _TaskPlannerState._readTaskList.<anonymous closure>
package:gatorblocks_rbt/pages/taskPlanner.dart:105
E/flutter ( 8249): #2 List.forEach (dart:core-patch/growable_array.dart:285:8)
E/flutter ( 8249): #3 _TaskPlannerState._readTaskList
package:gatorblocks_rbt/pages/taskPlanner.dart:104
E/flutter ( 8249): <asynchronous suspension>
E/flutter ( 8249): #4 _TaskPlannerState.initState
package:gatorblocks_rbt/pages/taskPlanner.dart:24
E/flutter ( 8249): #5 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4355
E/flutter ( 8249): #6 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #7 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #8 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #9 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #11 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #12 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #13 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #14 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #15 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #16 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #17 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #18 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #19 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #20 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #21 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #22 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #23 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #24 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #25 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #26 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #27 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4381
E/flutter ( 8249): #28 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #29 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #30 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #31 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #32 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #33 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #34 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #35 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #36 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #37 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #38 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #39 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #40 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4381
E/flutter ( 8249): #41 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #42 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #43 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #44 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #45 Element.rebuild (package:flutte
该应用程序运行良好,没有崩溃或任何其他问题,只是无法正确显示我保存到数据库中的项目。
您收到错误是因为您将 int
放入接受 String
.
的 Text
小部件中
您可以通过使用 toString()
方法转换 int 来解决这个问题。
检查下面的代码:将其替换为您的 handleSubmitted
函数:
void _handleSubmitted(String text) async {
_textEditingController.clear();
Tasks tasksItem = Tasks(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(tasksItem);
// convert savedItemId to a string using toString() method
print("Item saved ID: $savedItemId.toString()");
}
希望对您有所帮助。
已更新
通过您的代码后,我可以看到您正在尝试访问您未在 Tasks
构造函数中定义的字段 id
。
为什么会出现错误:
Type String is not a subtype of int index
希望这能解决您的错误。
我是 Flutter 的初学者,目前正在学习构建任务规划器的教程 (https://www.youtube.com/watch?v=xJSestoFlJ8&list=PLF9bFmu3NqWqchwS3DO9MDSl15IiHQWcr&index=9)
手动重写代码(数据库等)后,当我重新启动应用程序以在调试控制台中显示我保存到数据库中的项目时,我在使用 initstate 和 setstate 时收到错误提示
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
这是与错误相关的代码
final TextEditingController _textEditingController = TextEditingController();
var db = DatabaseHelper();
@override
void initState() {
super.initState();
_readTaskList();
}
void _handleSubmitted(String text) async {
_textEditingController.clear();
Tasks tasksItem = Tasks(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(tasksItem);
print("Item saved ID: $savedItemId");
}
_readTaskList() async {
List items = await db.getItems();
items.forEach((item) {
Tasks tasksItem = Tasks.map(items);
print("DB Items: ${tasksItem.itemName}");
});
}
这是完整的调试控制台日志
E/flutter ( 8249): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
E/flutter ( 8249): #0 new Tasks.map
package:gatorblocks_rbt/models/tasks.dart:12
E/flutter ( 8249): #1 _TaskPlannerState._readTaskList.<anonymous closure>
package:gatorblocks_rbt/pages/taskPlanner.dart:105
E/flutter ( 8249): #2 List.forEach (dart:core-patch/growable_array.dart:285:8)
E/flutter ( 8249): #3 _TaskPlannerState._readTaskList
package:gatorblocks_rbt/pages/taskPlanner.dart:104
E/flutter ( 8249): <asynchronous suspension>
E/flutter ( 8249): #4 _TaskPlannerState.initState
package:gatorblocks_rbt/pages/taskPlanner.dart:24
E/flutter ( 8249): #5 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4355
E/flutter ( 8249): #6 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #7 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #8 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #9 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #11 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #12 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #13 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #14 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #15 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #16 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #17 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #18 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #19 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #20 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #21 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #22 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #23 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #24 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #25 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #26 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #27 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4381
E/flutter ( 8249): #28 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #29 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #30 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #31 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #32 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #33 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #34 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5445
E/flutter ( 8249): #35 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #36 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #37 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #38 Element.rebuild
package:flutter/…/widgets/framework.dart:3947
E/flutter ( 8249): #39 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4206
E/flutter ( 8249): #40 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4381
E/flutter ( 8249): #41 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4201
E/flutter ( 8249): #42 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3194
E/flutter ( 8249): #43 Element.updateChild
package:flutter/…/widgets/framework.dart:2988
E/flutter ( 8249): #44 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4243
E/flutter ( 8249): #45 Element.rebuild (package:flutte
该应用程序运行良好,没有崩溃或任何其他问题,只是无法正确显示我保存到数据库中的项目。
您收到错误是因为您将 int
放入接受 String
.
Text
小部件中
您可以通过使用 toString()
方法转换 int 来解决这个问题。
检查下面的代码:将其替换为您的 handleSubmitted
函数:
void _handleSubmitted(String text) async {
_textEditingController.clear();
Tasks tasksItem = Tasks(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(tasksItem);
// convert savedItemId to a string using toString() method
print("Item saved ID: $savedItemId.toString()");
}
希望对您有所帮助。
已更新
通过您的代码后,我可以看到您正在尝试访问您未在 Tasks
构造函数中定义的字段 id
。
为什么会出现错误:
Type String is not a subtype of int index
希望这能解决您的错误。