Home > Enterprise >  Flutter Equivalent useProvider in new version of Riverpod
Flutter Equivalent useProvider in new version of Riverpod

Time:08-28

In previous version of riverpod we could use simply useProvider() and sending request to server without any action such as clicking on button, so in new version of riverpod i can't.

old version:

useEffect(() {
  myService.getService();
});

Scaffold(
  body: Container(
    width: double.infinity,
    height: double.infinity,
    color: Colors.white,
    child:
    useProvider(myServiceServiceProvider).when(
      idle: () {},
      loading: (){},
      success: (value){},
      error: (error, stackTrace){},
    ),
  ),
),

and my implementation in new version of riverpod:

class Home extends HookConsumerWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {

    //final future = ref.watch(dataProvider);
    useEffect(() {
      ref.read(requestProvider.notifier).send(
        method: HTTP.GET,
        endPoint: Server.$posts,
        parameters: {},
      );
      return () {};
    });

return Scaffold(
  body: Container(
    width: double.infinity,
    height: double.infinity,
    color: Colors.white,
    //HOW CAN IMPLEMENT THIS PART TO SHOW DATA IN LISTVIEW?
  ),
),

CodePudding user response:

Is this what you're looking for?

ref.watch(myServiceServiceProvider).when(
      idle: () {},
      loading: (){},
      success: (value){},
      error: (error, stackTrace){},
    )
  • Related