这是问题所在:应用程序在前台或后台(或终止(时都会收到通知,但在使用应用程序时通知的小吃栏不起作用。我是小蝶新手,所以也许我犯了什么大错误。
我在下面显示我的代码。如果您想了解更多信息,请询问并感谢您的帮助!
// Import Package
import 'dart:io';
import 'package:app_gap_go/pages/admin/modifySingleMeeting.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
// import 'package:flutter/rendering.dart';
// Import Other Package
import 'package:provider/provider.dart';
// Import Pagine
import './pages/wrapper.dart';
import './pages/setting/settings.dart';
import './pages/riunioni/riunioni.dart';
import './pages/auth/register.dart';
import './pages/admin/pannelloAdmin.dart';
import './pages/admin/modifyMeetings.dart';
// Import Utility
import 'utility/themeData.dart';
// Import Services
import './services/authService.dart';
// Import Models
import './models/user.dart';
void main() {
// debugPaintSizeEnabled = true;
// debugPaintBaselinesEnabled = true;
// debugPaintPointersEnabled = true;
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final FirebaseMessaging _fcm = new FirebaseMessaging();
final Firestore _db = Firestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
GlobalKey<ScaffoldState> scaffoldState = new GlobalKey<ScaffoldState>();
// Request and save token
Future _saveDeviceToken() async {
String fcmToken = await _fcm.getToken();
FirebaseUser user = await _auth.currentUser();
if (fcmToken != null) {
var tokenRef = _db
.collection('utenti')
.document(user.uid)
.collection('tokens')
.document(fcmToken);
await tokenRef.setData({
'token': fcmToken,
'createdAt': FieldValue.serverTimestamp(),
'platform': Platform.operatingSystem,
});
}
}
@override
void initState() {
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final snackbar = SnackBar(
content: Text(message['notification']['title']),
action: SnackBarAction(
label: 'Go',
onPressed: () => null,
),
);
scaffoldState.currentState.showSnackBar(snackbar);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
);
if(Platform.isIOS) {
_fcm.onIosSettingsRegistered.listen((data) {
_saveDeviceToken();
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
_fcm.subscribeToTopic('meetings');
super.initState();
}
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
key: scaffoldState,
value: AuthService().user,
child: MaterialApp(
title: "GapGo",
theme: themeApp,
routes: {
'/': (BuildContext context) => Wrapper(),
'/register': (BuildContext context) => Register(),
'/settings': (BuildContext context) => Settings(),
'/riunioni': (BuildContext context) => Riunioni(),
'/adminPanel': (BuildContext context) => PannelloAdmin(),
'/modifyMeetings': (BuildContext context) => ModifyMeetings(),
'/modifySingleMeeting': (BuildContext context) =>
ModifySingleMeeting(),
},
),
);
}
// End State
}
我确实遇到了同样的问题,我既不能使用对话框也不能使用小吃栏,问题实际上是由于没有导航器路线的当前上下文,这是我的解决方案。
更改以下内容:
SnackBarAction(
label: 'Go',
onPressed: () => null,
),
进入这个:
SnackBarAction(
label: 'Go',
onPressed: () => print("pressed"),
),
根据文档:
标签和 onPressed 参数必须为非 null。
https://api.flutter.dev/flutter/material/SnackBarAction/SnackBarAction.html
问题很可能是由于您在 StreamProvider.value 小部件中调用了 scaffoldState 键,但您的路由在该小部件之外。MaterialApp 小部件应该在里面并封装脚手架小部件。
我在这里有类似的情况,由于常量,SnackBarAction显示错误
**const** SnackBar(
content: Text('Added item to the Cart '),
duration: Duration(seconds: 2),
action: SnackBarAction(
label: 'UNDO',
onPressed: () {},
));
},
应该像这样
SnackBar(
content: Text('Added item to the Cart '),
duration: Duration(seconds: 2),
action: SnackBarAction(
label: 'UNDO',
onPressed: () {},
));
},