我正在尝试使用Firebase分析来跟踪用户导航,并按一些按钮,我创建了一个_currentScreen()
函数,该函数在用户导航到的类中称为初始化状态
这是功能
Future<Null> _currentScreen() async {
await widget.analytics.setCurrentScreen(
screenName: 'second screen view', screenClassOverride: 'SecondScreenView');
}
我创建了另一个函数_sendAnalytics,当用户单击按钮时调用该函数。这是函数:
Future<Null> _sendAnalytics() async {
await widget.analytics
.logEvent(name: 'launchhhh', parameters: <String, dynamic>{});
}
我有几个问题: 首先,在第二个屏幕类的initstate中,它警告:is方法将覆盖在“状态”中标注为@mustCallSuper的方法,但不会调用重写的方法。 2-当我运行代码并按导航按钮时,我就有了
[ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (12744): NoSuchMethodError: The method 'logEvent' was called on null.
E/flutter (12744): Receiver: null
E/flutter (12744): Tried calling: logEvent(name: "launchhhh", parameters: _LinkedHashMap len:0)
E/flutter (12744): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (12744): #1 FirstScreenState._sendAnalytics (file:///C:/Users/pc/Desktop/wedn2/wedn2/lib/h.dart:40:10)
3-另外,当我评论_sendanalytics函数
时NoSuchMethodError: The method 'setCurrentScreen' was called on null.
E/flutter (15612): Receiver: null
E/flutter (15612): Tried calling: setCurrentScreen(screenClassOverride: "SecondScreenView", screenName: "second screen view")
E/flutter (15612): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (15612): #1 SecondScreenState._currentScreen (file:///C:/Users/pc/Desktop/wedn2/wedn2/lib/h.dart:91:28)
E/flutter (15612): <asynchronous suspension>
E/flutter (15612): #2 SecondScreenState.initState (file:///C:/Users/pc/Desktop/wedn2/wedn2/lib/h.dart:88:5)
E/flutter (15612): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3787:58)
E/flutter (15612): #4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3653:5)
我不知道问题在哪里有帮助?这是我的整个代码:
import 'package:flutter/material.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'dart:async';
class MyAppf extends StatelessWidget {
static FirebaseAnalytics analytics = new FirebaseAnalytics();
static FirebaseAnalyticsObserver observer =
new FirebaseAnalyticsObserver(analytics: analytics);
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
navigatorObservers: <NavigatorObserver>[observer],
// home: new WallScreen(analytics: analytics, observer: observer),
home: new FirstScreen(),
);
}
}
class FirstScreen extends StatefulWidget {
final FirebaseAnalytics analytics;
final FirebaseAnalyticsObserver observer;
FirstScreen({this.analytics, this.observer});
@override
FirstScreenState createState() => new FirstScreenState();
}
class FirstScreenState extends State<FirstScreen> {
Future<Null> _sendAnalytics() async {
await widget.analytics
.logEvent(name: 'launchhhh', parameters: <String, dynamic>{});
}
final FirebaseAnalytics analytics;
final FirebaseAnalyticsObserver observer;
FirstScreenState({this.analytics, this.observer});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: RaisedButton(
child: Text('Launch screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
_sendAnalytics();
},
),
),
);
}
}
class SecondScreen extends StatefulWidget {
final FirebaseAnalytics analytics;
final FirebaseAnalyticsObserver observer;
SecondScreen({this.analytics, this.observer});
@override
SecondScreenState createState() => new SecondScreenState();
}
class SecondScreenState extends State<SecondScreen> {
@override
void initState() {
_currentScreen();
}
Future<Null> _currentScreen() async {
await widget.analytics.setCurrentScreen(
screenName: 'second screen view', screenClassOverride: 'SecondScreenView');
}
Future<Null> _sendAnalyticsback() async {
await widget.analytics
.logEvent(name: 'back', parameters: <String, dynamic>{});
}
final FirebaseAnalytics analytics;
final FirebaseAnalyticsObserver observer;
SecondScreenState({this.analytics, this.observer});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
_sendAnalyticsback();
},
child: Text('Go back!'),
),
),
);
}
}
EDIT 我将var分析传递给了Widgets
home:new FirstScreen(analytics: analytics),
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen(analytics:analytics)),
而不是:
home:new FirstScreen(),
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
但是调用null还是同样的错误
答案 0 :(得分:0)
您需要将var分析传递给小部件。
new FirstScreen(),
new SecondScreen(),
应该是:
new FirstScreen(analytics: analytics),
new SecondScreen(analytics: analytics),
答案 1 :(得分:0)
请勿使用静态成员,因为它们打算通过类本身进行访问。试图做某事的掩饰方法是将MyApp
声明为SatetfulWidget
,现在您analytics
和observer
可以是State
类的私有成员,并且使用initState
对其进行初始化,请参见以下示例:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
FirebaseAnalytics _analytics;
FirebaseAnalyticsObserver _observer ;
@override
void initState() {
_analytics = FirebaseAnalytics();
_observer = FirebaseAnalyticsObserver(analytics: _analytics);
super.initState();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
如果您想保留窗口小部件Stateless
,则可以在构造函数的初始化程序列表中初始化observer
,这要求analytics
字段在MyApp
时传递一个值已创建MyApp(analytics:FirebaseAnalytics())
:
class MyApp extends StatelessWidget {
final FirebaseAnalytics analytics;
final FirebaseAnalyticsObserver observer;
MyApp({Key key,@required this.analytics})
: observer = FirebaseAnalyticsObserver(analytics: analytics),
super(key: key);
@override
Widget build(BuildContext context) {
return Container();
}
}