![]() ![]() There are 2 API that we will use to create our example. In this blog, we will be putting all the things that we learned from the last two articles into the play and we will be creating two examples. We have discussed what Riverpod is and how it works, so if you haven't had a chance to read those posts, make sure to check them out first. If you have been following my Riverpod series, you know that we have covered a wide range of concepts related to this state management. final stringLabelProvider = Provider((ref) => 'Hello StringLabel(StringLabelRef ref)=>'Hello World of Riverpod' Ĭlass Home extends ConsumerWidget build(BuildContext context, WidgetRef ref) ' įinal newStringLabel = NotifierProvider(NewStringLabel.Welcome to my blog! In this blog, we will be exploring how to make an API call using Riverpod. So now our main.dart looks like below import 'package:riverpod_annotation/riverpod_annotation.dart' ![]() The is given as below flutter pub run build_runner watch -delete-conflicting-outputsįor the above command to run make sure you have installed the below plugin build_runner:Īfter running command look at the file below. We need to run a command to auto generate the code. You see we put the file name as part of our project and we put below other imports.Īt the top we have installed riverpod_generator and we need run the below comand Import 'package:flutter_riverpod/flutter_riverpod.dart' import 'package:riverpod_annotation/riverpod_annotation.dart' You need mention at the top right below other imports. In this case our function mentioned is in main.dart, so the auto generated file would be main.g.dart. The name should be similar as your current file name. ![]() You need to tell riverpod where to generate the file and you need to give it a name. √ run a special command to generate the provider √ mention where to put the auto generated provider code We are two more steps away from the auto generated code. Previously it was confusing to mention since there are six types of Providers. So Riverpod will auto generate a Provider that will help us manage a String.Įven though it looks like there's more code write this time, but this is still cool since you don't need to mention the Provider type on your own. In the above case, we need a Provider to manage our String. All you need to do it mention the return type of the function. Riverpod will auto generate the Provider for you. With this kind of syntax you don't need to mention the Providers you want. ![]() The above one is a function that would generate a Riverpod provider. Make sure you import annotation package file StringLabel(StringLabelRef ref)=>'Hello World of Riverpod' But how does it do it ? You need to give it more information, you need to provide it with a function. It's like below this Flutter would know that, it needs to generate riverpod code. Now we do the same using riverpod annotation. Here we return a Provider which gives us a String.īelow we will see how to generate providers in Riverpod 2.0 using code generation tools. The above providers is manually typed in and you mention the provider type. Previously we could have a Provider like below final stringLabelProvider = Provider((ref) => 'Hello World') At the same time make sure you install riverpod latest version at least equal to 2.0 or above. Since the new version depends a lot more on the auto generation of code, we will first see how to auto generate Providers with a package name riverpod_generator In this new version NotifierProvider and AysncNotifierProvider are encouraged to use more. You can still use the older version of Providers. Previous versions have seen six providers. It also introduced another two new providers. Pre look at Flutter Riverpod 2.0 from the above picture. It also introduced annotation and Riverpod code generator. It has also introduced two new providers NotifierProvider and AsyncNotifierProvider. It introduced auto generated providers which is convenient and less error prone. Riverpod 2.0 has big changes then the earlier versions. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |