Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

builder: (context, snapshot) {

>Solution :

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());  
  }                                                     
}),                                                      
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading