LateInitializationError - 颤动的生命周期
LateInitializationError - lifecyle of flutter
我正在学习 Flutter,现在正在测试一个应用程序以使用 SQLite。我想长期使用 firebase 运行,所以我正在构建代码以将令牌存储到数据库,但我认为我没有完全理解这个过程。
发生了 LateInitializationError,但随后它也打印了令牌,所以我想我搞砸了生命周期?
所以我的问题是,
- 为什么会报错然后打印出token?
- 如何将 tokenValue 数据存储到数据库中?
已经 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 中的函数。
谢谢!
来自
晚字符串令牌值;
到
字符串令牌值 = '';
会解决的。
我正在学习 Flutter,现在正在测试一个应用程序以使用 SQLite。我想长期使用 firebase 运行,所以我正在构建代码以将令牌存储到数据库,但我认为我没有完全理解这个过程。
发生了 LateInitializationError,但随后它也打印了令牌,所以我想我搞砸了生命周期?
所以我的问题是,
- 为什么会报错然后打印出token?
- 如何将 tokenValue 数据存储到数据库中?
已经 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 中的函数。
谢谢!
来自
晚字符串令牌值;
到
字符串令牌值 = '';
会解决的。