Reputation: 711
Since this morning I have been getting this error, after looking at the stack trace, I have traced the error back to the runApp method, I don't understand at all, what the problem is, any help would be appreciated :) This is the exception: The getter 'navigator' was called on null
Here is my main.dart file
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)
Upvotes: 2
Views: 2272
Reputation: 7488
You are getting this error because, the navigatorObserver instance "analyticsObserver" you are passing in your navigationObservers array is being instantiated in an async function, means, possibly when the build function is running for the first time, that time your "analyticsObserver" is null.
Try adding the following condition while assigning your navigationObservers array
navigatorObservers: (null == analyticsObserver)? [] : <NavigatorObserver>[analyticsObserver],
Or simply return circular progress indicator while analyticsObserver is null.
Upvotes: 5