I am new to flutter and I am studying Future Builder, when I run the code below the line: return snapshot.data; is returning the following error: The return type 'Object?' isn't a 'Widget', as required by the closure's context.
I figure this has something to do with Null Safety because when I change the line: return snapshot.data; to return Text(snapshot.data.toString()); it renders the text: ListView(ScrollDirection: vertical...ScrollPhysics) but I could be wrong about Null safety.
The code below is from a youtube tutorial: https://www.youtube.com/watch?v=y-IypschepA that I was watching, I copied the code word for word while learning and I already verified almost 20 times that my code is exactly what the tutor wrote and successfully ran.
I am not sure how to resolve this error.
import 'package:flutter/material.dart';
class FutureBuilderTest extends StatefulWidget {
@override
_FutureBuilderTestState createState() => _FutureBuilderTestState();
}
class _FutureBuilderTestState extends State<FutureBuilderTest> {
Future<void> loadList(BuildContext context) async {
return Future.delayed(
Duration(seconds: 1),
() {
return ListView.builder(
itemCount: 40,
itemBuilder: (context, index) {
return Card(
elevation: 8,
child: Text(
"Dummy Text $index",
style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
);
},
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Future Builder Test"),
),
body: FutureBuilder(
future: loadList(context),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.connectionState == ConnectionState.done) if (snapshot
.hasError) {
return Center(
child: Text(snapshot.data.toString()),
);
}
if (snapshot.data != null) {
return snapshot.data; // THIS LINE IS RETURNING THE ERROR
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
}