Skip to content

Instantly share code, notes, and snippets.

@eduardoflorence
Created December 24, 2020 20:03
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save eduardoflorence/b4bca2da5cfb973b9f86ecfa1b9f013a to your computer and use it in GitHub Desktop.
Save eduardoflorence/b4bca2da5cfb973b9f86ecfa1b9f013a to your computer and use it in GitHub Desktop.
GetX - Sample GetConnect
import 'package:flutter/material.dart';
import 'package:get/get.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.lazyPut(() => CityProvider());
Get.put(CityController(cityProvider: Get.find()));
}
}
class CityModel {
CityModel({
this.abbreviation,
this.name,
});
String abbreviation;
String name;
factory CityModel.fromJson(Map<String, dynamic> json) => CityModel(
abbreviation: json["sigla"],
name: json["nome"],
);
static List<CityModel> listFromJson(list) =>
List<CityModel>.from(list.map((x) => CityModel.fromJson(x)));
}
class CityController extends GetxController with StateMixin<List<CityModel>> {
final CityProvider cityProvider;
CityController({this.cityProvider});
@override
void onInit() {
findAllCities();
super.onInit();
}
void findAllCities() {
cityProvider.getCity().then((result) {
List<CityModel> data = result.body;
change(data, status: RxStatus.success());
}, onError: (err) {
change(null, status: RxStatus.error(err.toString()));
});
}
void insertCity() {
const body = {'nome': 'joao', 'idade': 47};
cityProvider.postCity(body).then((result) {
print(result.body.abbreviation);
print(result.body.name);
});
}
}
class CityProvider extends GetConnect {
@override
void onInit() {
// All request will pass to jsonEncode so CasesModel.fromJson()
httpClient.defaultDecoder = CityModel.listFromJson;
httpClient.addRequestModifier((request) {
request.headers['Authorization'] = 'Bearer sdfsdfgsdfsdsdf12345678';
return request;
});
}
Future<Response<List<CityModel>>> getCity() => get<List<CityModel>>(
'https://servicodados.ibge.gov.br/api/v1/localidades/estados'
);
Future<Response<CityModel>> postCity(body) =>
post<CityModel>('http://192.168.0.101:3000/items', body,
decoder: (obj) => CityModel.fromJson(obj));
}
class CityPage extends GetView<CityController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Cities')),
body: Container(
child: controller.obx(
(state) => ListView.builder(
itemCount: state.length,
itemBuilder: (context, index) {
return Text(state[index].name);
},
),
onLoading: Center(child: CircularProgressIndicator()),
onError: (error) => Center(
child: Text(
'Error: $error',
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => controller.insertCity(),
),
);
}
}
@nisone
Copy link

nisone commented Sep 17, 2021

Hello I'm just getting started with getx package but I wasn't sure how to use two controllers on one GetView class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment