自从今天早上以来,我一直得到这个错误,在查看了堆栈跟踪之后,我将错误追溯到了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;
});
}
}
Logcat
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)
答案 0 :(得分:1)
您收到此错误,因为在异步函数中实例化了您在navigationObservers数组中传递的navigatorObserver实例“ analyticsObserver”,这意味着,可能是当构建函数首次运行时,您的“ analyticsObserver” “为空。
尝试在分配navigationObservers数组时添加以下条件
navigatorObservers: (null == analyticsObserver)? [] : <NavigatorObserver>[analyticsObserver],
或者在analyticsObserver为null时简单地返回循环进度指示器。