Home > Blockchain >  I have an error while parsing json data over internet in flutter
I have an error while parsing json data over internet in flutter

Time:01-23

This is my code and i am having an error in the "builder": while opening the curly braces it shows error like

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

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.hasData) {
              return const Center(
                child: Text('Data Ok'),
              );
            } else 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, index) {
                  return Container(
                    child: ListTile(
                      title: Text(
                        snapshot.data![index],
                      ),
                    ),
                  );
                },
              ));
            }
          },
          future: ebdetails(),
        ),
      ),
    );
  }
}

I have pasted the error line below for reference
at the end while opening curly braces it shows error

builder: (context, snapshot) {

CodePudding user response:

You need to add an "else" statement so that all possible condition are covered and the builder always returns something:

FutureBuilder(builder: (context, snapshot) {            
  if (!snapshot.hasData) {                              
    return const Center(                                
      child: Text('Data Ok'),                           
    );                                                   
  } else 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, index) {                   
        return Container(                               
          child: ListTile(                              
            title: Text(                                
              snapshot.data![index],                    
            ),                                           
          ),                                             
        );                                               
      },                                                
    ));                                                  
  } else {   // Add This                                           
    return Center(child: CircularProgressIndicator());  
  }                                                     
}),                                                      

CodePudding user response:

You have to check three status of snapshot like below:

  1. For success snapshot result widget.
  2. For error widget.
  3. Waiting for data - show progress indicator
 FutureBuilder(

          future: ebdetails(),

          builder: (context, snapShot) {

            if (snapShot.hasData) { 

              return Container();  // your success widget
              

            } else if (snapShot.hasError) {

              return const Text("Error");   // your error widget
             
            }

            return const CircularProgressIndicator();

          }),
  • Related