如何修复在Flutter上不包含导航器的上下文中请求的导航器操作

时间:2020-07-23 00:34:52

标签: android ios flutter push-notification firebase-cloud-messaging

我在main.dart上有以下代码

// import 'package:cotapp/utils/PushNotificationManager.dart';
import 'package:cotapp/utils/item.dart';
import 'package:cotapp/views/notificationDetails.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';

import 'package:cotapp/views/index.dart';
import 'package:cotapp/views/politics.dart';
import 'package:cotapp/views/register.dart';
import 'package:cotapp/views/home.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  // PushNotificationsManager pushNotificationsManager = PushNotificationsManager();
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  final Map<String, Item> _items = <String, Item>{};


  Item _itemForMessage(Map<String, dynamic> message) {
    final dynamic data = message['data'] ?? message;
    final String itemId = data['id'];
    final Item item = _items.putIfAbsent(itemId, () => Item(itemId: itemId))
      ..status = data['status'];
    return item;
  }

  @override
  void initState() {
    super.initState();
    _prefs.then((SharedPreferences prefs) {
      this.loadToken(prefs);
    });
    _firebaseMessaging.configure(
      // onBackgroundMessage: (Map<String, dynamic> message) async{
      //   print(message);
      // },
      onLaunch: (Map<String, dynamic> message) async {
        print('onLaunch: $message');
        print('************************************************************');
        _navigateToDetail(message);
      },
      onResume: (Map<String, dynamic> message) async {
        print('onResume: $message');
        print('************************************************************');
        _navigateToDetail(message);
      },
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        // _showItemDialog(message);
      }
    );
  }

  void _navigateToDetail(Map<String, dynamic> message) {
    try {
      final Item item = _itemForMessage(message);
      // Clear away dialogs
      Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
      if (!item.route.isCurrent) {
        Navigator.push(context, item.route);
      }
    } catch(error) {
      print(error);
    }
  }

  void loadToken(SharedPreferences prefs) async{
    // if(prefs.getString('token') == null) {
    String token = await _firebaseMessaging.getToken();
    prefs.setString('token=>', token);
    // }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cotapp',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(fontFamily: 'Montserrat'),
      initialRoute: '/',
      routes: {
        '/': (context) => Index(),
        '/politics': (context) => Politics(),
        '/register': (context) => Register(),
        '/home': (context) => Home(),
        '/details': (context) => NotificationDetails(),
      },
    );
  }
}

还有一个单独文件名为item.dart的类

import 'package:cotapp/views/notificationDetails.dart';
import 'package:flutter/material.dart';
import 'dart:async';

class Item {
  Item({this.itemId});
  final String itemId;

  StreamController<Item> _controller = StreamController<Item>.broadcast();
  Stream<Item> get onChanged => _controller.stream;

  String _status;
  String get status => _status;
  set status(String value) {
    _status = value;
    _controller.add(this);
  }

  static final Map<String, Route<void>> routes = <String, Route<void>>{};
  Route<void> get route {
    final String routeName = '/details';
    return routes.putIfAbsent(
      routeName,
      () => MaterialPageRoute<void>(
        settings: RouteSettings(name: routeName),
        builder: (BuildContext context) => NotificationDetails(),
      ),
    );
  }
}

但是当onResume或onLaunch函数触发并尝试导航到详细信息视图时,出现此错误

请求的导航器操作具有不包含导航器的上下文

我在做什么错?感谢您的帮助

0 个答案:

没有答案