SHARE
TWEET

Products+mobx+provider

multiarts Jan 21st, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ProductsController = _ProductsBase with _$ProductsController;
  2.  
  3. abstract class _ProductsBase with Store {
  4.   @observable
  5.   List<ProductsModel> productsModel = [];
  6.  
  7.   @observable
  8.   List<ProductsModel> list = [];
  9.  
  10.   @computed
  11.   int get totalItems => productsModel.length;
  12.  
  13.   @observable
  14.   bool loading = false;
  15.  
  16.   @action
  17.   fetchProducts() async {
  18.     try {
  19.       final response = await Dio().get(Api.getProducts);
  20.       final decodeJson = jsonDecode(response.data) as List;
  21.  
  22.       // print(decodeJson);
  23.  
  24.       for (Map i in decodeJson){
  25.         list.add(ProductsModel.fromJson(i));
  26.       }
  27.       return decodeJson;
  28.     } catch (error, stacktrace) {
  29.       print("Exceção ocorrida: $error stacktrace: $stacktrace");
  30.       return null;
  31.     }
  32.   }
  33.  
  34.   @action
  35.   Future<List<ProductsModel>> loadProducts() async {
  36.     final response = await Dio().get(Api.getProducts);
  37.     final data = jsonDecode(response.data);
  38.     final List<ProductsModel> pList = [];
  39.     for(Map i in data){
  40.       pList.add(ProductsModel.fromJson(i));
  41.     }
  42.     return pList;
  43.   }
  44.   dispose(){}
  45. }
  46.  
  47. // HomePage
  48.  
  49. class _HomePageState extends State<HomePage> {
  50.   @override
  51.   Widget build(BuildContext context) {
  52.  
  53.     double statusBarHeight = MediaQuery.of(context).padding.top;
  54.     double widthScreen = MediaQuery.of(context).size.width;
  55.     double heightScreen = MediaQuery.of(context).size.height;
  56.  
  57.     final _productController = Provider.of<ProductsController>(context);
  58.     _productController.loadProducts();
  59.  
  60.     // final controller = ProductsController();
  61.     // controller.fetchProducts();
  62.  
  63.     return Scaffold(
  64.       appBar: AppBar(
  65.         title: Observer(builder: (_){
  66.           return Text('Produtos ' + _productController.totalItems.toString());
  67.         },),
  68.       ),
  69.       body: Container(
  70.         height: heightScreen - 108 - statusBarHeight,
  71.         color: Colors.red,
  72.         child: Observer(
  73.           builder: (_) => _productController.productsModel != null
  74.           ? ListView.builder(
  75.               physics: BouncingScrollPhysics(),
  76.               padding: EdgeInsets.all(12),
  77.               addAutomaticKeepAlives: false,
  78.               itemCount: _productController.totalItems,
  79.               itemBuilder: (_, index) {
  80.                 final item = _productController.productsModel[index];
  81.                 return Container(
  82.                   child: Text('OK '+
  83.                     item.productName
  84.                   ),
  85.                 );
  86.               },
  87.           )
  88.           : Center(
  89.             child: CircularProgressIndicator(),
  90.           )
  91.         )
  92.       ),
  93.     );
  94.   }
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top