skip to Main Content

I want to ask about get the data in rapidapi. Previously I had succeeded in retrieving data from Rapidapi in a form like the image below, I retrieved the data in the default article:

Example get the data in my previous project

Map data = jsonDecode(response.body);
    // ignore: no_leading_underscores_for_local_identifiers
    List _temp = [];

    for (var i in data['results']) {
      _temp.add(i['defaultArticle']);
    }
    

    return Clothes.clothesFromSnapshot(_temp);`

and now I’m confused about retrieving the data I want to retrieve now because the form of the JSON is different. How to retrieve the data shows?

Json form that i want to get the data now (shows)

2

Answers


  1.         - Json Data / API Data
                  please visit following link : https://reqres.in/api/users?page
            
            
            - Fetch Data
               void fetchData() async{
                try{
                 final response = await Dio().get('https://reqres.in/api/users?page');
                 if(response.statusCode == 200){
                  data = List<DataModel>.from((response.data['data']).map((e) => DataModel.fromJson(e)).toList());
                } 
              }catch(e){
               print("Exception ===> $e");
              }
            }
        
             
            
            - View data
               ListView.builder(
                      itemCount: data.length,
                      itemBuilder: (context, index){
                        return ListTile(
                          leading: Text("${data[index].id}"),
                          title: Text("${data[index].firstName} ${data[index].lastName}"),
                          subtitle: Text("${data[index].email}"),
                        );
                      }
                  ),
    
    
    
    
    
    - summary.
    
    1. Error hadling : use try - catch to handle error.   
    2. Async-await : using async-await handle asynchronous event.   
    3. DIO libraries : Dio is a more feature-rich library compared to http. It provides more functionality out of the box, such as request cancellation, interceptors, and request/response transformation.    
    4. Check the status code : 200 means OK.    
    5. Json data extraction : the code extracts the particular JSON data from the response using response.data['data'].
    6. Data model conversion : The code converts the JSON data to a list of DataModel objects using the fromJson method. This is a good practice because it allows you to work with strongly-typed data models in your app, rather than raw JSON data.
    7. Assign List : List<type>.from() method is the best way to assign one list to other list.
    
    Login or Signup to reply.
  2. If you have a JSON array where each object has a dynamic name in Flutter, you can’t directly access these objects without knowing their names. However, you can access them by iterating over the keys and values of each object. Try with this way:

    String jsonData = '[{"0": {"id": 3}}, {"1": {"id": 4}}, {"2": {"id": 5}}, {"3": {"id": 8}}, {"4": {"id": 10}}]';
        
          List<dynamic> data = jsonDecode(jsonData);
        
          for (var obj in data) {
            obj.forEach((key, value) {
              String dynamicName = key;
              int id = value['id'];
              print('Dynamic Name: $dynamicName, ID: $id');
            });
          }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search