Home > Blockchain >  I am a flutter beginner while getting json data from internet it is not showing in my app.. Solve it
I am a flutter beginner while getting json data from internet it is not showing in my app.. Solve it

Time:01-23

This is my complete code and i don't know what kind of error i have made
Correct it if you know, as i am beginner in flutter.

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Mydata(),
    );
  }
}

class Mydata extends StatefulWidget {
  const Mydata({super.key});
  @override
  State<Mydata> createState() => _MydataState();
}

class _MydataState extends State<Mydata> {
  Future<List<String>> ebdetails() async {
    var response =
        await http.get(Uri.parse('http://117.247.181.113:8000/eb/1/'));
    return jsonDecode(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        elevation: 0,
        centerTitle: true,
        title: const Text(
          'Json Datas',
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        backgroundColor: Colors.white,
      ),
      body: Center(
        child: FutureBuilder(
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return const Center(
                child: Text('Data Error'),
              );
            } else if (snapshot.hasData) {
              return Center(
                  child: ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, i) {
                  return Text(
                    snapshot.data![i],
                  );
                },
              ));
            } else {
              return const CircularProgressIndicator();
            }
          },
          future: ebdetails(),
        ),
      ),
    );
  }
}

I am fetching data over local ip but it seems to have some sort of error i have made, but its hard for me to find..
It shows only "Data error which i have given in Futurebuilder..

[
    {
        "id": 1,
        "R_Current": -1.0,
        "Y_Current": -1.0,
        "B_Current": -1.0,
        "R_Voltage": 208,
        "Y_Voltage": 235,
        "B_Voltage": 208,
        "UPS_Voltage": 100,
        "UPS_Current": 143.0,
        "UPS_Battery": 99
    }
]

CodePudding user response:

so according to your data it is not the list of the String change the return type of the method to this

Future<List<dynamic>> ebdetails() async {
var response =
    await http.get(Uri.parse('http://117.247.181.113:8000/eb/1/'));
return jsonDecode(response.body);
}

CodePudding user response:

I have changed the code and got the answer i.e snapshot.data![i].toString()

builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Center(
                  child: ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, i) {
                  return Text(
                    snapshot.data![i].toString(),
                  );
  • Related