【发布时间】:2021-11-17 11:00:36
【问题描述】:
我正在学习 Flutter,现在正在测试一个应用程序以使用 SQLite。从长远来看,我想使用 firebase,所以我正在构建代码来将令牌存储到数据库中,但我认为我并没有完全理解这个过程。
发生了 LateInitializationError,但它也打印了令牌,所以我想我搞砸了生命周期?
所以我的问题是,
- 为什么会出现错误然后打印出token?
- 如何将 tokenValue 数据存储到 DB 中?
已经 2 个月了,仍然没有掌握 Flutter...
+编辑 这是放置空值后的错误消息
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Builder:
Class 'Future<dynamic>' has no instance method 'insertToken'.
Receiver: Instance of 'Future<dynamic>'
Tried calling: insertToken(Instance of 'Token')
The relevant error-causing widget was:
MaterialApp file:///C:/Users/katej/Desktop/firem6_demo/lib/main.dart:17:5
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _MyAppState.initState (package:firem6_demo/main.dart:49:15)
#2 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57)
#3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
... Normal element mounting (166 frames)
...
====================================================================================================
I/flutter ( 9579): (token)
I/flutter ( 9579): []
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Builder:
Class 'Future<dynamic>' has no instance method 'insertToken'.
Receiver: Instance of 'Future<dynamic>'
Tried calling: insertToken(Instance of 'Token')
The relevant error-causing widget was:
MaterialApp file:///C:/Users/katej/Desktop/firem6_demo/lib/main.dart:17:5
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _MyAppState.initState (package:firem6_demo/main.dart:49:15)
#2 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57)
#3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
... Normal element mounting (4 frames)
...
这是 main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
MaterialApp(home: MyApp()),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FirebaseMessaging messaging;
late String tokenValue;
void copyToken() {
(() {
if (tokenValue != null) {
Clipboard.setData(ClipboardData(text: tokenValue));}
}());
}
@override
void initState() {
messaging = FirebaseMessaging.instance;
messaging.getToken().then((value) {
tokenValue = value!;
copyToken();
print(tokenValue);
});
tokenDb();
var user1 = Token(token: tokenValue);
tokenDb().insertToken(user1);
tokenDb().updateToken(user1);
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "DemoApp",
home: BottomBarScreen(),
routes: {
MonitoringScreen.routeName: (context) => MonitoringScreen(),
GuideScreen.routeName: (context) => GuideScreen(),
PracticeScreen.routeName: (context) => PracticeScreen(),
SettingsScreen.routeName: (context) => SettingsScreen()
},
);
}
}
这是 db_test.dart,里面有 tokenDb() 函数。
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
tokenDb() async {
final database = openDatabase(
join(await getDatabasesPath(), 'token_list.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE tokens (token INTEGER PRIMARY KEY)",
);
},
version: 1,
);
Future<void> insertToken(Token token) async {
final Database db = await database;
await db.insert(
'tokens',
token.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Future<List<Token>> tokens() async {
final Database db = await database;
final List<Map<String, dynamic>> maps = await db.query('tokens');
return List.generate(maps.length, (i) {
return Token(
token: maps[i]['token']
);
});
}
Future<void> updateToken(Token token) async {
final db = await database;
await db.update(
'tokens',
token.toMap(),
where: "id = ?",
whereArgs: [token.token],
);
}
Future<void> deleteToken(int id) async {
final db = await database;
await db.delete(
'tokens',
where: "id = ?",
whereArgs: [id],
);
}
print(await tokens());
}
class Token {
String? token;
Token({this.token});
Map<String, dynamic> toMap() {
final map = Map<String, dynamic>();
map['token'] = token;
return map;
}
factory Token.fromMap(Map<String, dynamic> map) {
return Token(token: map['token']);
}
}
在 Main initState() 内部,tokenDb().insertToken(user1) 不起作用; insertToken 没有指向 tokenDb 中的函数。
谢谢!
【问题讨论】:
-
你能发布错误日志吗?
-
@EngineSense 我编辑了这篇文章!
-
是的!这个错误是有道理的,很明显你必须用一些空值初始化字符串。
-
@EngineSense 真的很抱歉,但是当我输入一个空值时,它又抛出了另一个异常;构建 Builder 时引发了以下 NoSuchMethodError:类“Future
”没有实例方法“insertToken”。接收者:'Future '的实例尝试调用:insertToken('Token'的实例) -
更新错误日志
标签: database firebase flutter sqlite initialization