在 null 上调用了 getter 'navigator'

Mus*_*man 2 flutter

从今天早上开始我一直收到这个错误,在查看堆栈跟踪后,我将错误追溯到runApp方法,我完全不明白,问题是什么,任何帮助将不胜感激:) 这是例外:getter 'navigator' 在 null 上被调用

这是我的 main.dart 文件

import 'dart:async';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:flutter/material.dart';
import 'package:savings/utils/analytics.dart';
import 'package:savings/utils/colors.dart';
import 'package:savings/pages/goals_page.dart';
import 'package:savings/utils/logic.dart' as AppLogic;
import 'package:savings/utils/models.dart' as models;
import 'package:savings/utils/variables.dart';
import 'package:flutter/services.dart';

void main() => runApp(new SavingsApp());

class SavingsApp extends StatefulWidget {
 @override
 _SavingsAppState createState() => new _SavingsAppState();
}

class _SavingsAppState extends State<SavingsApp> {
 @override
 void initState() {
 initAppData();
 super.initState();
}

 @override
 Widget build(BuildContext context) {
  return new MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'Savings',
  color: primaryThemeColor,
  home: GoalsPage(),
  navigatorObservers: <NavigatorObserver>[analyticsObserver],
  theme: new ThemeData(
    primaryColor: primaryThemeColor,
    accentColor: accentThemeColor,
    fontFamily: "Proxima Nova",
  ),
);
}

@override
void dispose() {
 //Good practice to close the DB
 AppLogic.DatabaseInterface.closeDB();

//Handle orientation
SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeRight,
  DeviceOrientation.landscapeLeft,
  DeviceOrientation.portraitUp,
  DeviceOrientation.portraitDown,
]);
super.dispose();
}

 Future<Null> initAppData() async {

//Database
_initDBData();

//Analytics
analytics = new FirebaseAnalytics();
AnalyticsInterface.logAppOpen();
analyticsObserver = new FirebaseAnalyticsObserver(analytics: analytics);
  }

  Future<Null> _initDBData() async {
   //Orientation
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);

//Retrieve database from the DB interface
appDatabase = await AppLogic.DatabaseInterface.getDB();

//Log output
print("DatabaseInterface: DatabaseInitiated");

//Get a local version of the lists after the DB has been initiated
List<models.Goal> _goalsList =
    await AppLogic.DatabaseInterface.getAllGoalsFromDB();

List<models.Transaction> _transactionsList =
    await AppLogic.DatabaseInterface.getAllTransactionsFromDB();

//Call setState and update the global lists (which are used in the whole app) from the local ones
setState(() {
  allGoalsList = _goalsList;
  allTransactionsList = _transactionsList;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

逻辑猫

The following NoSuchMethodError was thrown building 
DefaultTextStyle(debugLabel: fallback style;
I/flutter ( 4211): consider putting your text in a Material, inherit: true, 
color: Color(0xd0ff0000), family:
I/flutter ( 4211): monospace, size: 48.0, weight: 900, decoration: double 
Color(0xffffff00) TextDecoration.underline,   
I/flutter ( 4211): softWrap: wrapping at box width, overflow: clip):
I/flutter ( 4211): The getter 'navigator' was called on null.
I/flutter ( 4211): Receiver: null
I/flutter ( 4211): Tried calling: navigator
I/flutter ( 4211): 
I/flutter ( 4211): When the exception was thrown, this was the stack:
I/flutter ( 4211): #0      Object.noSuchMethod 
(dart:core/runtime/libobject_patch.dart:46:5)
I/flutter ( 4211): #1      NavigatorState.initState 
(package:flutter/src/widgets/navigator.dart:1303:23)
I/flutter ( 4211): #2      StatefulElement._firstBuild 
(package:flutter/src/widgets/framework.dart:3751:58)
I/flutter ( 4211): #3      ComponentElement.mount 
(package:flutter/src/widgets/framework.dart:3617:5)
...
...
I/flutter ( 4211): #104    runApp (package:flutter/src/widgets/binding.dart:704:7)
I/flutter ( 4211): #105    main (file:///C:/Coding/savings/lib/main.dart:13:16)
I/flutter ( 4211): #106    _startIsolate.<anonymous closure (dart:isolate/runtime/libisolate_patch.dart:279:19)
I/flutter ( 4211): #107    _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
Run Code Online (Sandbox Code Playgroud)

Gan*_*pat 5

您收到此错误是因为,您在 navigationObservers 数组中传递的 navigatorObserver 实例“analyticsObserver”正在异步函数中实例化,这意味着,可能当构建函数第一次运行时,您的“analyticsObserver”为空.

在分配您的 navigationObservers 数组时尝试添加以下条件

navigatorObservers: (null == analyticsObserver)? [] : <NavigatorObserver>[analyticsObserver],
Run Code Online (Sandbox Code Playgroud)

或者在 analyticsObserver 为空时简单地返回循环进度指示器。