-1
Good guys? I’m a beginner in flutter, and I’m working on a ready-made template. In this template I have the class Category.Dart that receives the json values of service.Dart and so far everything is ok. What I need to do is call the Category class data into the Appdata class in the data.Dart file, thus replacing the fixed sample data with the dynamic data.
Service.Dart
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:../src/model/category.dart';
class Connection {
static const ROOT = 'http://localhost/get_category.php';
static const _GET_ALL_ACTION = 'GET_ALL';
static const _ADD_CAT_ACTION = 'ADD_CAT';
static const _UPDATE_CAT_ACTION = 'UPDATE_CAT';
static const _DELETE_CAT_ACTION = 'DELETE_CAT';
static Future<List<Category>> getCategories() async {
try {
var map = Map<String, dynamic>();
map['acao'] = _GET_ALL_ACTION;
final response = await http.post(ROOT, body: map);
print('getCategories -> Response :: ${response.body}');
if(response.statusCode == 200) {
List<Category> list = parseResponse(response.body);
return list;
}else{
return List<Category>();
}
}catch (e) {
return List<Category>();
}
}
static List<Category> parseResponse(String response) {
final parsed = json.decode(response).cast<Map<String, dynamic>>();
return parsed.map<Category>((json) => Category.fromJson(json)).toList();
}
Category.Dart
class Category{
String id ;
String name ;
String image ;
String description;
bool isSelected;
Category({this.id,this.name,this.image, this.description, this.isSelected});
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
id: json['cod'] as String,
name: json['nome'] as String,
image: json['thumb'] as String,
description: json['descricao'] as String,
);
}
}
data.Dart
import 'package:flutter/material.dart';
import 'package:klug_ml/src/model/category.dart';
class AppData {
static List<Category> categoryList = [
Category() //quero chamar os dados da categoria aqui
//Os dados dinâmicos, devem carregar como o exemplo abaixo.
Category(id:1,name: "Sneakers",image: 'assets/shoe_thumb_2.png',isSelected: true),
Category(id:2,name: "Jacket", image: 'assets/jacket.png'),
Category(id:3,name: "Watch", image: 'assets/watch.png'),
];
}
Call of the Appdata Class in the project.
Widget _categoryWidget() {
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: AppTheme.fullWidth(context),
height: 80,
child: ListView(
scrollDirection: Axis.horizontal,
children: AppData.categoryList
.map(
(category) => ProductIcon(
model: category,
onSelected: (model) {
setState(() {
AppData.categoryList.forEach((item) {
item.isSelected = false;
});
model.isSelected = true;
});
},
),
)
.toList(),
),
);
}
Your search code in the API then is all right the way you want it isn’t? The problem you don’t know how to do is just call the method that fetches the data from your API? If this is all within your Appdata class, there needs to be a method that calls the API. It will get the list ready for what is in its implementation. The data from your Category will all come ready. You don’t have to do anything else, you’re just gonna use them. If this is not your question comment here below the part that did not understand so I change or even remove my published answer.
– Leonardo Paim
That’s exactly it, I’m not sure how to call the data into Static List<Category> categoryList = [ ]
– Marcio Woppe