Created
December 13, 2020 16:55
-
-
Save eduardoflorence/07e1049583b8e723e69e86f1fff2156e to your computer and use it in GitHub Desktop.
GetX - Sample GetNotifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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