在flutter中处理链接并使用Firebase动态链接进行导航

时间:2020-02-26 17:51:19

标签: firebase flutter dart navigation flutter-dependencies

我在flutter项目应用程序中使用firebase_dynamic_links 0.5.0。我正在使用bloc进行状态管理,并且导航出现问题。我通常在处理链接,但是当我尝试导航到另一个不起作用的页面时。我不知道为什么?

PS:我已经测试过firebase_dynamic_links example,并且可以正常工作


main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Production code
  ErrorWidget.builder = (FlutterErrorDetails details) => Container();


  runApp(MyApp());
}

// Myapp类,我得到打印结果,但导航不起作用

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _authBloc = AuthBloc();

  final _progressBloc = new ProgressBloc();

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



  void initDynamicLinks() async {
    final PendingDynamicLinkData data =
        await FirebaseDynamicLinks.instance.getInitialLink();
    final Uri deepLink = data?.link;

    //print("hhhhhhh" + deepLink.path);

    if (deepLink != null) {
      print("Link :" + deepLink.path);

      // this line don't work 
      Navigator.pushNamed(context, '/helloworld');
    }

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
      final Uri deepLink = dynamicLink?.link;

      if (deepLink != null) {
        print("Link :" + deepLink.path);

        Navigator.pushNamed(context, '/helloworld');
      }
    }, onError: (OnLinkErrorException e) async {
      print('onLinkError');
      print(e.message);
    });
  }

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = ThemeData(
      fontFamily: Constants.DEFAULT_FONT_NAME,
      textTheme: Theme.of(context)
          .textTheme
          .apply(fontFamily: Constants.DEFAULT_FONT_NAME),
      brightness: Brightness.light,
      canvasColor: Colors.black12,
      scaffoldBackgroundColor: Colors.white,
      primaryIconTheme:
          Theme.of(context).primaryIconTheme.copyWith(color: Colors.white),
      accentIconTheme:
          Theme.of(context).accentIconTheme.copyWith(color: Colors.white),
      primaryTextTheme:
          Theme.of(context).primaryTextTheme.apply(bodyColor: Colors.white),
      primaryColorLight: Colors.white,
      primaryColorBrightness: Brightness.light,
      primarySwatch: Constants.COLOR_PRIMARY,
      primaryColor: Constants.COLOR_PRIMARY,
    );

    var home = Stack(children: <Widget>[
      RootPage(),
      ProgressDialog(),
    ]);

    return ProgressProvider(
      dialog: _progressBloc,
      child: AuthBlocProvider(
        bloc: _authBloc,
        child: StoreBlocProvider(
          store: Store(),
          child: RootProvider(
            bloc: new RootBloc(),
            child: MaterialApp(
              localizationsDelegates: [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('en'), // English
                const Locale('fr'), // Hebrew
              ],
              title: 'Foodynasty',
              theme: themeData,
              debugShowCheckedModeBanner: false,
              routes: <String, WidgetBuilder>{
                '/': (BuildContext context) => home,
                '/helloworld': (BuildContext context) => RestaurantShare(
                      restaurantId: 'test',
                    ),
              },
            ),
          ),
        ),
      ),
    );
  }
}

谢谢,对不起,我的英语不好。

1 个答案:

答案 0 :(得分:0)

可能是deepLink为空,并且if语句中的代码未执行。您能否提供一个发送到应用程序的深层链接的示例?如果发生错误,firebase_dynamic_links可能无法正确解析深层链接。