flutter 零安全误差|未处理异常:类型“Null”不是类型“String”的子类型

ycl3bljg  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(151)

我们正在尝试使用以下链接https://dart.dev/null-safety/migration-guide将flutter项目升级到null安全迁移。
dart pub outdated --mode=null-safety - Successful dart pub get - Successful手动迁移代码-https://dart.dev/null-safety/migration-guide#migrating-by-hand- Successful dart migrate - Successful
尝试运行Flutter命令时,以下代码中出现错误(Flutter运行)

static Future<AppStrings> getAppText(String local) async {
    var appTextBox = await Hive.openBox("appTextBox");
    AppStrings appText = new AppStrings(
      mainPageTitle: appTextBox.get('mainPageTitle'),
      timeText: appTextBox.get('timeText'),
      minutesText: appTextBox.get('minutesText'),
      mainPageSubtitle: appTextBox.get('mainPageSubtitle'),
      mainPageParagraph: appTextBox.get('mainPageParagraph'),
      mainPageButton: appTextBox.get('mainPageButton'),
      restartButton: appTextBox.get('restartButton'),
      backButton: appTextBox.get('backButton'),
      profileTitle: appTextBox.get('profileTitle'),
      loadingPageSubtitle: appTextBox.get('loadingPageSubtitle'),
      loadingPageBottomText: appTextBox.get('loadingPageBottomText'),
      loadingPageTitle: appTextBox.get('loadingPageTitle'),
      profileButton: appTextBox.get('profileButton'),
      resultRestart: appTextBox.get('resultRestart'),
      resultTitle: appTextBox.get('resultTitle'),
      resultSwipe: appTextBox.get('resultSwipe'),
      secondaryTitle: appTextBox.get('secondaryTitle'),
      tretiaryProductTitle: appTextBox.get('tertiaryTitle'),
    );
    return appText;
  }

更新:-
这就是我们如何从main.dart文件调用这个方法

AppStrings? appText = (await DatabaseUtils.getAppText(local ?? ''));
  return Data(
      allProfiles: allProfiles,...........
)
vmjh9lq9

vmjh9lq91#

更改为getAppText(String?local)或getAppText(String local??'')

o8x7eapl

o8x7eapl2#

试试这个:

static Future<AppStrings?> getAppText(String local) async {
   var appTextBox = await Hive.openBox("appTextBox");
   AppStrings? appText = new AppStrings(
     mainPageTitle: appTextBox.get('mainPageTitle'),
     timeText: appTextBox.get('timeText'),
     minutesText: appTextBox.get('minutesText'),
     mainPageSubtitle: appTextBox.get('mainPageSubtitle'),
     mainPageParagraph: appTextBox.get('mainPageParagraph'),
     mainPageButton: appTextBox.get('mainPageButton'),
     restartButton: appTextBox.get('restartButton'),
     backButton: appTextBox.get('backButton'),
     profileTitle: appTextBox.get('profileTitle'),
     loadingPageSubtitle: appTextBox.get('loadingPageSubtitle'),
     loadingPageBottomText: appTextBox.get('loadingPageBottomText'),
     loadingPageTitle: appTextBox.get('loadingPageTitle'),
     profileButton: appTextBox.get('profileButton'),
     resultRestart: appTextBox.get('resultRestart'),
     resultTitle: appTextBox.get('resultTitle'),
     resultSwipe: appTextBox.get('resultSwipe'),
     secondaryTitle: appTextBox.get('secondaryTitle'),
     tretiaryProductTitle: appTextBox.get('tertiaryTitle'),
   );

   return appText;
 }

并尝试像上面显示的那样离开呼叫。

相关问题