0

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) {

1
  • all snapshot if , else if might be gone false. So you need to add return widget in last when all if , else if not true. Commented Jan 22, 2023 at 15:13

2 Answers 2

1

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());  
  }                                                     
}),                                                      
Sign up to request clarification or add additional context in comments.

1 Comment

Saved it @Joe Muller
1

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();

          }),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.