slovnicki / beamer

A routing package built on top of Router and Navigator's pages API, supporting arbitrary nested navigation, guards and more.
MIT License
582 stars 128 forks source link

pass and retrieve data and getting NULL #630

Closed pishguy closed 11 months ago

pishguy commented 11 months ago

I am using the Beamer library in one of my projects, and I cannot pass a value to another location. For example, I wrote this code to navigate:

Beamer.of(context).beamToNamed('/homePage/chefStoreHomePage', 
   data: 
     {
       'id': 9
     }
);

Here, I expect to pass an object named id with a value of 9, and then receive it in the implementation code below:

class ALocation extends BeamLocation<BeamState> {
  ALocation(super.routeInformation);

  @override
  List<String> get pathPatterns => ['/*', '/a/details:id'];

  @override
  List<BeamPage> buildPages(BuildContext context, BeamState state) {
    List<BeamPage> pages = [];
    pages.add(
      const BeamPage(
        key: ValueKey('a'),
        title: 'Tab A',
        type: BeamPageType.noTransition,
        child: RootScreen(label: 'A', detailsPath: '/a/details'),
      ),
    );

    if (state.uri.pathSegments.length == 2) {
      pages.add(
        const BeamPage(
          key: ValueKey('a/details'),
          title: 'Details A',
          type: BeamPageType.noTransition,
          child: DetailsScreen(label: 'A'),
        ),
      );
    }

    return pages;
  }
}

screen and pass data:

class RootScreen extends StatelessWidget {
  const RootScreen({required this.label, required this.detailsPath, Key? key})
      : super(key: key);
  final String label;
  final String detailsPath;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tab root - $label'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Padding(padding: EdgeInsets.all(4)),
            TextButton(
              onPressed: () => Beamer.of(context).beamToNamed('$detailsPath/1'),
              child: const Text('View details'),
            ),
          ],
        ),
      ),
    );
  }
}

Here, I need to receive the number and assign it to id:

int id= int.tryParse(state.pathParameters['id']!);

And in the ChefStoreWidget class, I want to receive this value using the context:

var note = context.currentBeamLocation.state.data['id'];

However, I always receive null in all of my attempts to receive the value.