Home > front end >  The body might cause 'null' to be returned, but the return type Widget is a potentially no
The body might cause 'null' to be returned, but the return type Widget is a potentially no

Time:08-19

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.

I am currently using:

environment:
  sdk: ">=2.17.6 <3.0.0"

in my pubspec.yaml

My code for reference:

import 'package:algorithm_learn/sqldb.dart';
import 'package:flutter/material.dart';

class Home extends StatefulWidget {
const Home({super.key});

@override
State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
SqlDb sqlDb = SqlDb();

Future<List<Map>> readData() async {
List<Map> response = await sqlDb.readData("SELECT * FROM 'Coing'");
return response;
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text('HomePage'),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: () {
      Navigator.of(context).pushNamed("addperson");
    },
    child: const Icon(Icons.add),
  ),
  body: Container(
    child: ListView(
      children: [
        // MaterialButton(
        //   onPressed: () async {
        //     await sqlDb.mydeleteDatabase();
        //   },
        //   child: const Text("Delete Database"),
        // ),
        FutureBuilder(
            future: readData(),
            builder:
                (BuildContext context, AsyncSnapshot<List<Map>>   snapshot) {     (ERROR IS HERE)
              if (snapshot.hasData) {
                return ListView.builder(
                  itemCount: snapshot.data!.length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (context, i) {
                    return Card(
                      child: ListTile(
                        title: Text("${snapshot.data![i]['first_name']}"),
                        subtitle: Text("${snapshot.data![i]['last_name']}"),
                        trailing: Text("${snapshot.data![i]['color']}"),
                      ),
                    );
                  },
                );
              }
            })
      ],
    ),
  ),
 );
}
}

I tried adding '!' which ensures that there won't be a 'null' value, but that doesn't work it only makes it worse.

I also tried changing the environment to:

environment:
  sdk: ">=2.17.6 <3.0.0"

but that makes things worse too.

CodePudding user response:

The builder of future builder will require a widget to be returned. You added an if condition with a return. But if it doesnt go inside this condition then there is no widget thats returned

Try adding an else condition and return a widget. Maybe a circularprogressIndicator or Sizedbox.shrink()

FutureBuilder(
            future: readData(),
            builder:
                (BuildContext context, AsyncSnapshot<List<Map>> ((ERROR IS HERE))  snapshot) {
              if (snapshot.hasData) {
                return ListView.builder(
                  itemCount: snapshot.data!.length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (context, i) {
                    return Card(
                      child: ListTile(
                        title: Text("${snapshot.data![i]['first_name']}"),
                        subtitle: Text("${snapshot.data![i]['last_name']}"),
                        trailing: Text("${snapshot.data![i]['color']}"),
                      ),
                    );
                  },
                );
              }
            else{
             return SizedBox.shrink();//<---here
           }
            })

CodePudding user response:

Add an else clause and return some widget as Text('No data found') or something

  • Related