csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

[Improvement] set or use url strategy #340

Closed Pr47h4m closed 2 years ago

Pr47h4m commented 2 years ago

I've similar issue [issue] in my case I'm using firebase as backend my main.dart file looks like:

import 'firebase_options.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:firebase_core/firebase_core.dart';

import 'exports/e_main.dart'; // all other imports are exported here

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(Travel());
}

class Travel extends StatelessWidget {
  Travel({Key? key}) : super(key: key);

  final Future<FirebaseApp> _initialization = Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          // return error page
          return const SomethingWentWrong();
        }
        if (snapshot.connectionState == ConnectionState.done) {
          // return app
          return ChangeNotifierProvider(
            create: (context) => PAuth(),
            child: const App(),
          );
        }
        // return loading
        return const Loading();
      },
    );
  }
}

So here, first MaterialApp will be from Loading Widget But its not possible for MyApplication to add GoRouter in Loading Widget I tried setting url strategy explicitly but then go router don't understand paths correctly

I have a suggestion

There should be following 2 properties in GoRouter

1) setUrlStrategy

2) useUrlStrategy

If setUrlStrategy is not null then set and use specified url strategy else useUrlStrategy will only be used to resolve path

csells commented 2 years ago

You should be able to call GoRouter.setUrlPathStrategy in main as described in the docs:

void main() {
  // turn off the # in the URLs on the web
  GoRouter.setUrlPathStrategy(UrlPathStrategy.path);

  runApp(App());
}