-1
import 'package:flutter/material.dart';
import 'package:notes/db/database_provider.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
     initialRoute: "/",
     routes: {
       "/":(context)=>HomeScreen()
     },
    );
  }
}
class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  getNotes()async{
    final notes = await DatabaseProvider.db.getNotes();
    return notes;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Notes"),
      ),
      body: FutureBuilder(
        future: getNotes(),
        builder: (context,noteData){
          switch(noteData.connectionState){
            case ConnectionState.waiting:
              {
                return Center(child: CircularProgressIndicator(),);
              }
            case ConnectionState.done:
              {
                if(noteData.data !=null){
                  return Padding(
                    padding:  const EdgeInsets.all(8.0),
                    child: ListView.builder(
                      itemCount:noteData.data!.length,
                      itemBuilder: (context,index){
                        String title=noteData.data[index]['title'];
                      },

                    ),
                  );
                }

                else {

                    return Center(
                      child:  Text("You don't have any notes yet,create one?"),
                    );

                }
              }
          }
        },
      ),
    );
  }
}



So, I am making a notes app using SQF lite taking guidance from DocorCode on youtube, i wrote the same code but its showing some errors related to null checks and i am unable to bypass them not matter what.

no matter what I do add null checks or anything it always shows error , is there anyway to switch off the null check , Please tell me how to fix the problem.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

0

The issue arise on title=noteData.data[index]['title'] because it is possible to get null on data. As you've checked null on top level, you can use ! here or better approach is providing default value based on cases.

String title = noteData.data![index]['title'];
String title = noteData.data?[index]['title']?? "default text";

As for the FutureBuilder including data type provide more flexibility. Replace int with your model and you can make it nullable or just pass empty list from getNotes.
Future<List<Note>> getNotes() async {...}

Useing FutureBuilder inside StatefulWidget will recall the api on on setState(state change). Better use initState

Provide a default return from switch case, including .hasError is a good choice.

      body: FutureBuilder<List<int>>(
        future: getNotes(),
        builder: (context, noteData) {
          switch (noteData.connectionState) {
            case ConnectionState.waiting:
              {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            case ConnectionState.done:
              {
                if (noteData.hasData) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: ListView.builder(
                      itemCount: noteData.data?.length,
                      itemBuilder: (context, index) {
                        // String title = noteData.data![index]['title'];
                        String title = noteData.data![index].toString();

                        return Text(title);
                      },
                    ),
                  );
                } else {
                  return const Center(
                    child: Text("You don't have any notes yet,create one?"),
                  );
                }
              }

            default:
              return CircularProgressIndicator();
          }
        },
      ),

Explore more about

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56