0

I know this is a basic question but I new to flutter . I have a stream builder which returns me a json which has maps nested in lists. How do I access each of these fields to create widgets to display on screen. please help

this is the code


import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';


void main() async {
runApp(MaterialApp(home: PeriodicRequester(),));
}



class PeriodicRequester extends StatelessWidget {
Stream<http.Response> getRandomNumberFact() async* {
yield* Stream.periodic(Duration(seconds: 5), (_) {
  return http.get(Uri.parse("https://script.google.com/macros/s/AKfycbwhbpF4ZxuMUcTZZvObAqvE1pAbEfPt7gZHRV1vVp8PuKt39-ouOm-kQJ1U1LtlEwV-/exec"));
}).asyncMap((event) async => await event);
}

@override
Widget build(BuildContext context) {
return StreamBuilder<http.Response>(
  stream: getRandomNumberFact(),
  builder: (context, snapshot) => snapshot.hasData
      ? Center(child: Text(snapshot.data!.body))
      : CircularProgressIndicator(),
);
}
}

  • Does this answer your question? [How to decode JSON in Flutter?](https://stackoverflow.com/questions/51601519/how-to-decode-json-in-flutter) – user18309290 Jul 24 '22 at 04:26

1 Answers1

0

You can convert the data tojson and use it like the following

.

return StreamBuilder<http.Response>(
  stream: getRandomNumberFact(),
  builder: (context, snapshot) { 
    
    if(snapshot.hasData)
    { 
       var convertedData = json.decode(snapshot.data);
      return Column( 
         children: List.generate(convertedData.length, (index) => Text(convertedData[index]['id']),
     )
    }
  }
);
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30