未处理的异常-Flutter

时间:2019-02-08 02:35:40

标签: dart flutter

我是新手,可以通过youtube上有关notodo_app的教程来学习。因此我遵循相同的代码,并且已经尝试重写几乎所有内容,但是当我开始使用initstate,setstate时。

与setstate相关的代码

final List<NoDoItem> _itemList = <NoDoItem>[];

@override
void initState() {
 super.initState();
 _readNoDoList();
}


void _handleSubmitted(String text) async {
 _textEditingController.clear();

 NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
 int savedItemId = await db.saveItem(noDoItem);
 NoDoItem addedItem = await db.getItem(savedItemId);
 setState(() {
  _itemList.insert(0, addedItem); 
 });
 print("Item saved id: $savedItemId");
}

_readNoDoList() async {
 List items = await db.getItems();
 items.forEach((item) {
   NoDoItem noDoItem = NoDoItem.map(item);
   print("Db items: ${noDoItem.itemName}");
 });
}

我收到此错误,并且没有提交任何东西,如果使用的是模拟器,它将自动关闭。

E/flutter (23633): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (23633): Stack Overflow
E/flutter (23633): #0      _Completer.completeError (dart:async/future_impl.dart:19:3)
E/flutter (23633): #1      _RootZone.runBinary (dart:async/zone.dart:1384:54)
E/flutter (23633): #2      _FutureListener.handleError (dart:async/future_impl.dart:143:20)
E/flutter (23633): #3      Future._propagateToListeners.handleError (dart:async/future_impl.dart:654:47)
E/flutter (23633): #4      Future._propagateToListeners (dart:async/future_impl.dart:675:24)
E/flutter (23633): #5      Future._completeError (dart:async/future_impl.dart:494:5)
E/flutter (23633): #6      _SyncCompleter._completeError (dart:async/future_impl.dart:55:12)
E/flutter (23633): #7      _Completer.completeError (dart:async/future_impl.dart:27:5)
E/flutter (23633): #8      _RootZone.runBinary (dart:async/zone.dart:1384:54)
E/flutter (23633): #9      _FutureListener.handleError (dart:async/future_impl.dart:143:2

1 个答案:

答案 0 :(得分:0)

您遵循的示例的 database_client.dart 文件存在错误,特别是导致递归/堆栈溢出异常。请将第22行上的代码从 return db; 更新为 return _db; ,它现在应该可以正常工作

Future<Database> get db async {
    if (_db != null) {
      return _db; // return db; throws a Stack Overflow Exception / recursion
    }
    _db = await initDb();
    return _db;
  }