onesignal:打开通知后导航至屏幕。我收到此错误:构建期间调用setState()或markNeedsBuild()

时间:2019-02-09 18:00:31

标签: redux flutter onesignal navigator

场景:
打开单信号通知时,我需要打开特定的屏幕。

这是我首先尝试的:

@NgModule

此代码的问题是,当我打开通知时,什么也没有发生。
即使我在import ...; void main() async { final persistor = Persistor<AppState>( storage: FlutterStorage(), serializer: JsonSerializer<AppState>(AppState.fromJson), ); final initialState = await persistor.load(); if (initialState.isLoggedIn) { initialState.silentlyLogin(); } final store = Store<AppState>( appReducer, /* Function defined in the reducers file */ initialState: initialState ?? AppState.initial(), middleware: [persistor.createMiddleware()], ); FlutterError.onError = (FlutterErrorDetails details) async { if (isInDebugMode) { FlutterError.dumpErrorToConsole(details); } else { Zone.current.handleUncaughtError(details.exception, details.stack); } }; intl_local_date_data.initializeDateFormatting(); Intl.defaultLocale = 'it_IT'; OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none); var settings = { OSiOSSettings.autoPrompt: false, OSiOSSettings.promptBeforeOpeningPushUrl: true }; await OneSignal.shared.init("...", iOSSettings: settings); OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification); runZoned<Future<void>>(() async { runApp(MyApp(store: store)); }, onError: (error, stackTrace) { _reportError(error, stackTrace); }); } class MyApp extends StatefulWidget { final Store<AppState> store; const MyApp({Key key, this.store}) : super(key: key); @override _MyAppState createState() => new _MyAppState(); } class _MyAppState extends State<MyApp> { @override void initState() { super.initState(); initPlatformState(); } Future<void> initPlatformState() async { if (!mounted) return; OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {}); OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) { // NOTHING HAPPENS: THE HOMESCREEN IS OPENED ANYWAY Navigator.push( context, MaterialPageRoute( builder: (context) => AnotherScreen(), ), ); }); } @override Widget build(BuildContext context) { return new StoreProvider<AppState>( store: widget.store, child: new MaterialApp( initialRoute: '/', routes: { '/': (context) => HomeScreen(), } ) ); } } 函数中添加了Navigate.push调用,该应用程序仍会打开主屏幕。

所以我尝试使用另一种方法
每当我打开通知时,我都会调度redux操作,并在主屏幕中管理它以打开另一个屏幕。

我用以下方法修改了setNotificationOpenedHandler函数:

setNotificationOpenedHandler

该动作已正确调度。在主屏幕中,我尝试使用以下代码来更改redux阶段:

OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
    widget.store.dispatch(new Action(result.notification.payload.additionalData["id"]));
});

使用此代码,我得到这个错误:

import ...

class HomeScreen extends StatefulWidget {

  HomeScreen({Key key}) : super(key: key);

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

class _HomeScreenState extends State<HomeScreen> {

  @override
  void initState() {
    super.initState();
  }

  //...

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppState>(
      converter: (store) => store.state,
      builder: (context, appState) {

        // HERE I MANAGE CHANGE OF REDUX STATE TO NAVIGATE
        if (appState.id != null) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => AnotherScreen(),
            ),
          );
        }

        return DefaultTabController(
          length: 3,
          // ecc...
        );
      },
    );
  }
}

0 个答案:

没有答案