首页 > 解决方案 > LateInitializationError - 颤振的生命周期

问题描述

我正在学习 Flutter,现在正在测试一个使用 SQLite 的应用程序。从长远来看,我想使用firebase,所以我正在构建代码来将令牌存储到数据库中,但我认为我并没有完全理解这个过程。

LateInitializationError 发生了,但它也打印了令牌,所以我想我搞砸了生命周期?

所以我的问题是,

  1. 为什么会发生错误然后打印出令牌?
  2. 如何将 tokenValue 数据存储到数据库中?

已经2个月了,仍然没有掌握颤振的窍门......

+Edit 这是放置空值后的错误消息

======== 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 中的函数。

谢谢你!

标签: databasefirebasefluttersqliteinitialization

解决方案


后期字符串令牌值;

字符串令牌值 = '';

会解决的。


推荐阅读