Compare commits
No commits in common. "8e58ae5503b5602c0214a9183db2ffd522ad882d" and "d44fc9a75c88a6bb6e87b87dd25050833d93bf53" have entirely different histories.
8e58ae5503
...
d44fc9a75c
@ -1,10 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
import 'package:riverpod_flutter_tuts/widgets/notifier/counter_widget.dart';
|
|
||||||
|
|
||||||
import '../widgets/consumer/random.dart';
|
|
||||||
import '../widgets/notifier/random_widget.dart';
|
|
||||||
import '../widgets/simples/random.dart';
|
import '../widgets/simples/random.dart';
|
||||||
|
|
||||||
part 'gt.g.dart';
|
part 'gt.g.dart';
|
||||||
@ -43,10 +40,7 @@ class MyApp extends ConsumerWidget {
|
|||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
const Expanded(child: Text('1')),
|
const Expanded(child: Text('1')),
|
||||||
Expanded(child: Text(value)),
|
Expanded(child: Text(value)),
|
||||||
const Expanded(child: RandomWidget()),
|
const Expanded(child: RandomWidget())
|
||||||
const Expanded(child: Random2Widget()),
|
|
||||||
const Expanded(child: CounterWidget()),
|
|
||||||
const Expanded(child: Random3Widget()),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ part 'getRandomData.g.dart';
|
|||||||
@riverpod
|
@riverpod
|
||||||
Future<MyRandom> getMyRandom(GetMyRandomRef ref) async {
|
Future<MyRandom> getMyRandom(GetMyRandomRef ref) async {
|
||||||
// Using package:http, we fetch a random activity from the Bored API.
|
// Using package:http, we fetch a random activity from the Bored API.
|
||||||
var ur = Uri.http('127.0.0.1:3000', '/p/random');
|
var ur = Uri.http('192.168.160.207:3000', '/p/random');
|
||||||
final response = await http.get(ur);
|
final response = await http.get(ur);
|
||||||
// Using dart:convert, we then decode the JSON payload into a Map data structure.
|
// Using dart:convert, we then decode the JSON payload into a Map data structure.
|
||||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
||||||
import 'package:riverpod_flutter_tuts/outerapi/auth/call/getRandomData.dart';
|
|
||||||
import 'package:riverpod_flutter_tuts/outerapi/auth/model/my_random.dart';
|
|
||||||
|
|
||||||
class Random2Widget extends ConsumerWidget {
|
|
||||||
const Random2Widget({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
|
||||||
// TODO: implement build
|
|
||||||
|
|
||||||
final AsyncValue<MyRandom> myRandom = ref.watch(getMyRandomProvider);
|
|
||||||
return Center(
|
|
||||||
child: switch (myRandom) {
|
|
||||||
AsyncData(:final MyRandom value ) => Text(
|
|
||||||
'myRandom: $value'
|
|
||||||
),
|
|
||||||
AsyncError() => const Text('Oops, something unexpected happened'),
|
|
||||||
_ => const CircularProgressIndicator(),
|
|
||||||
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
||||||
|
|
||||||
|
|
||||||
part 'counter.g.dart';
|
|
||||||
|
|
||||||
@riverpod
|
|
||||||
class CounterNotifier extends _$CounterNotifier {
|
|
||||||
|
|
||||||
int x = 0;
|
|
||||||
@override
|
|
||||||
Future<int> build() async{
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
void plus() {
|
|
||||||
x++;
|
|
||||||
ref.invalidateSelf();
|
|
||||||
// state = AsyncData(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
void minus() {
|
|
||||||
x--;
|
|
||||||
// ref.invalidateSelf();
|
|
||||||
state = AsyncData(x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
||||||
import 'counter.dart';
|
|
||||||
|
|
||||||
|
|
||||||
class CounterWidget extends ConsumerWidget {
|
|
||||||
const CounterWidget({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
|
||||||
// TODO: implement build
|
|
||||||
|
|
||||||
var counter = ref.watch(counterNotifierProvider);
|
|
||||||
return Column(
|
|
||||||
children: [
|
|
||||||
Center(
|
|
||||||
child: switch (counter) {
|
|
||||||
AsyncData(:final value ) => Text(
|
|
||||||
'data: $value'
|
|
||||||
),
|
|
||||||
AsyncError() => const Text('Oops, something unexpected happened'),
|
|
||||||
_ => const CircularProgressIndicator(),
|
|
||||||
}),
|
|
||||||
ElevatedButton(onPressed:() {
|
|
||||||
ref.read(counterNotifierProvider.notifier).plus();
|
|
||||||
}, child: const Text("plus")),
|
|
||||||
ElevatedButton(onPressed: () {
|
|
||||||
ref.read(counterNotifierProvider.notifier).minus();
|
|
||||||
}, child: const Text("minus")),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
||||||
import 'package:riverpod_flutter_tuts/outerapi/auth/model/my_random.dart';
|
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
part 'random.g.dart';
|
|
||||||
|
|
||||||
Future<MyRandom> fetchData() async {
|
|
||||||
var ur = Uri.http('127.0.0.1:3000', '/p/random');
|
|
||||||
final response = await http.get(ur);
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@riverpod
|
|
||||||
class randomNotif extends _$randomNotif {
|
|
||||||
@override
|
|
||||||
Future<MyRandom> build() async{
|
|
||||||
return fetchData();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> again() async {
|
|
||||||
state = const AsyncLoading();
|
|
||||||
// ref.invalidateSelf();
|
|
||||||
ref.invalidateSelf();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
||||||
|
|
||||||
import 'random.dart';
|
|
||||||
|
|
||||||
class Random3Widget extends ConsumerWidget {
|
|
||||||
const Random3Widget({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
|
||||||
// TODO: implement build
|
|
||||||
|
|
||||||
var myRandom = ref.watch(randomNotifProvider);
|
|
||||||
return Row(
|
|
||||||
children: [
|
|
||||||
Center(
|
|
||||||
child: switch (myRandom) {
|
|
||||||
AsyncData(:final value ) => Text(
|
|
||||||
'data: $value'
|
|
||||||
),
|
|
||||||
AsyncError() => const Text('Oops, something unexpected happened'),
|
|
||||||
_ => const CircularProgressIndicator(),
|
|
||||||
}),
|
|
||||||
!myRandom.isLoading ? ElevatedButton(onPressed:() {
|
|
||||||
ref.read(randomNotifProvider.notifier).again();
|
|
||||||
}, child: Text(myRandom.isLoading ? "wait":"new"))
|
|
||||||
:
|
|
||||||
const Text("loading")
|
|
||||||
,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:riverpod_flutter_tuts/outerapi/auth/call/getRandomData.dart';
|
import 'package:riverpod_flutter_tuts/outerapi/auth/call/getRandomData.dart';
|
||||||
@ -17,24 +16,15 @@ class RandomWidget extends StatelessWidget {
|
|||||||
|
|
||||||
final AsyncValue<MyRandom> myRandom = ref.watch(getMyRandomProvider);
|
final AsyncValue<MyRandom> myRandom = ref.watch(getMyRandomProvider);
|
||||||
|
|
||||||
return Row( children: <Widget>[
|
return Center(
|
||||||
Center(
|
child: switch (myRandom) {
|
||||||
child: switch (myRandom) {
|
AsyncData(:final MyRandom value ) => Text(
|
||||||
AsyncData(:final MyRandom value ) => Text(
|
'myRandom: $value'
|
||||||
'myRandom: $value'
|
),
|
||||||
),
|
AsyncError() => const Text('Oops, something unexpected happened'),
|
||||||
AsyncError() => const Text('Oops, something unexpected happened'),
|
_ => const CircularProgressIndicator(),
|
||||||
_ => const CircularProgressIndicator(),
|
|
||||||
|
|
||||||
},
|
},
|
||||||
),
|
|
||||||
ElevatedButton(
|
|
||||||
onPressed: () {
|
|
||||||
ref.invalidate(getMyRandomProvider);
|
|
||||||
},
|
|
||||||
child: const Text("Refresh ME"),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user