Skip to content

Instantly share code, notes, and snippets.

@febritecno
Forked from eduardoflorence/main.dart
Created June 6, 2022 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save febritecno/a62e75cd1dd73cabb6ade0de2ae54856 to your computer and use it in GitHub Desktop.
Save febritecno/a62e75cd1dd73cabb6ade0de2ae54856 to your computer and use it in GitHub Desktop.
GetX - Sample GetNotifier
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart';
void main() {
runApp(GetMaterialApp(
initialRoute: '/home',
getPages: [
GetPage(
name: '/home',
page: () => HomePage(),
),
GetPage(
name: '/city',
page: () => CityPage(),
binding: CityBinding(),
),
],
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('HOME')),
body: Center(
child: RaisedButton(
onPressed: () => Get.toNamed('/city'),
child: Text('Go to Cities'),
),
),
);
}
}
class CityBinding extends Bindings {
@override
void dependencies() {
Get.put(Dio());
Get.put(CityController(dio: Get.find()));
}
}
class StateModel {
StateModel({
this.sigla,
this.nome,
});
String sigla;
String nome;
factory StateModel.fromJson(Map<String, dynamic> json) => StateModel(
sigla: json["sigla"],
nome: json["nome"],
);
Map<String, dynamic> toJson() => {
"sigla": sigla,
"nome": nome,
};
static List<StateModel> listFromJson(list) =>
List<StateModel>.from(list.map((x) => StateModel.fromJson(x)));
}
/* class CityController extends GetxController with StateMixin<List<StateModel>> {
final Dio dio;
CityController({this.dio});
@override
void onInit() {
super.onInit();
const String url =
'https://servicodados.ibge.gov.br/api/v1/localidades/estados';
dio.get(url).then((result) {
List<StateModel> data = StateModel.listFromJson(result.data);
change(data, status: RxStatus.success());
}, onError: (err) {
change(null, status: RxStatus.error(err.toString()));
});
}
} */
class CityController extends GetNotifier<List<StateModel>> {
final Dio dio;
// What differentiates GetNotify from StateMix, is the need to start
// the Model with a value, that is, the constructor is mandatory
CityController({this.dio, List<StateModel> initial})
: super(initial = [StateModel(sigla: 'AA', nome: 'estado1')]);
getStates() {
change(null, status: RxStatus.loading());
const String url =
'https://servicodados.ibge.gov.br/api/v1/localidades/estados';
dio.get(url).then((result) {
List<StateModel> data = StateModel.listFromJson(result.data);
change(data, status: RxStatus.success());
}, onError: (err) {
change(null, status: RxStatus.error(err.toString()));
});
}
}
class CityPage extends GetView<CityController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Cities')),
body: Container(
child: controller.obx(
(state) => Column(
children: [
RaisedButton(
color: Colors.blue,
onPressed: () => controller.getStates(),
splashColor: Colors.blueGrey,
child: Text(
'Search Cities',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
),
Expanded(
child: ListView.builder(
itemCount: state.length,
itemBuilder: (context, index) {
return Text(state[index].nome);
},
),
),
],
),
onLoading: Center(child: CircularProgressIndicator()),
onError: (error) => Center(
child: Text(
error,
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment