88 lines
3.8 KiB
Plaintext
Raw Normal View History

2024-06-05 00:42:08 +03:30
1. https://riverpod.dev/docs/introduction/getting_started
1.1. commands run in terminal
>>
flutter pub add flutter_riverpod
flutter pub add riverpod_annotation
flutter pub add dev:riverpod_generator
flutter pub add dev:build_runner
flutter pub add dev:custom_lint
flutter pub add dev:riverpod_lint
<<
1.2.
>>
flutter pub get
flutter pub run build_runner build ( instead of flutter pub run build_runner watch)
<<
1.3. Add Links.txt file.
1.4. Checkout in other station ==> Must re run 1.1. steps again. (I don't Know why)
1.5. After a while, i read the document. Now updated some dependencies and starting
use riverpod again.
Now I know many things about it, but riverpod learning curve is not good.
Flutter was better but not good at result. I will stick on learning curve before
developing the app.
2. https://riverpod.dev/docs/essentials/first_request
2.1. Install freezed using this commands:
(using https://github.com/rrousselGit/freezed?tab=readme-ov-file#install)
>>
flutter pub add freezed_annotation
flutter pub add dev:build_runner
flutter pub add dev:freezed
flutter pub add json_annotation
flutter pub add dev:json_serializable
<<
install flutterX & FlutterAssetsGenerator & freezed plugins.
2.2. Add model for utc and random endpoints.
Do these steps for any JSON, request or response.
Use snippet freezedClass, then use @freezed menu, then "Json From Freezed"
then paste,then click the button --> then change class name, file name, path then save.
Now paste in this
import 'package:flutter/foundation.dart';
to generated file.
Now use @freezed menu, then "Run Build Runner".
2.3. Run command
>>
flutter pub add http
<<
2.4. New file then paste this code. For random API. (GET http://localhost:3000/p/random)
>>
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../model/my_random.dart';
// Necessary for code-generation to work
part 'provider.g.dart';
/// This will create a provider named `activityProvider`
/// which will cache the result of this function.
@riverpod
Future<MyRandom> getMyRandom(GetMyRandomRef ref) async {
// Using package:http, we fetch a random activity from the Bored API.
final response = await http.get(Uri.https('localhost:3000', '/p/random'));
// Using dart:convert, we then decode the JSON payload into a Map data structure.
final json = jsonDecode(response.body) as Map<String, dynamic>;
// Finally, we convert the Map into an Activity instance.
return MyRandom.fromJson(json);
}
<<
Check the pattern should be with this format:
@riverpod
Result myFunction(MyFunctionRef ref) {
<your logic here>
}
myFunction and MyFunctionRef
xabcd and XabcdRef
2.5. Create a widget with data.
it is simple using these line of code
final AsyncValue<MyRandom> myRandom = ref.watch(getMyRandomProvider);
that serves data as a aysncvalue.
there is a sample in widgets/simples folder.
Note: Continue from "Going further" in "https://riverpod.dev/docs/essentials/first_request"
2024-07-14 19:09:25 +03:30
Note: After checkout, just run
flutter pub get
flutter pub run build_runner build