I took a course about Flutter from udemy. I've come to the Firebase part. However, I am getting errors in this part. I am getting an error even though I type exactly the same. I think I got error like this because there are updates in Flutter language. I'm sorry my English is bad. Thank you in advance for helping me.
main.dart
import 'dart:html';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fire_base1/user.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<List<User>> getData() async {
final res = await FirebaseFirestore.instance.collection("users").get();
List<User> myresult = <User>[];
for (int i = 0; i < res.docs.length; i ) {
User user = new User(
name: res.docs[i]["name"],
last_name: res.docs[i]["last_name"],
email: res.docs[i]["email"]);
myresult.add(user);
}
return myresult;
}
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter ;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("FireBase"),
),
body: FutureBuilder(
future: Firebase.initializeApp(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("Veritabanına bağlanırken bir hata meydana geldi");
}
if (snapshot.connectionState == ConnectionState.done) {
return FutureBuilder(
future: getData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data[index].name
" "
snapshot.data[index].last_name),
subtitle: Text(snapshot.data[index].email),
);
},
);
} else {
return SizedBox();
}
},
);
}
return Center(
child: CircularProgressIndicator(),
);
},
));
}
}
user.dart
class User {
String name;
String last_name;
String email;
User({required this.name, required this.last_name, required this.email});
Map<String, dynamic> toMap() {
Map<String, dynamic> data = new Map<String, dynamic>();
data["name"] = name;
data["last_name"] = last_name;
data["email"] = email;
return data;
}
}
CodePudding user response:
Please always copy/paste the error your IDE or your runtime shows in here. Screenshots won't help much.
Anyways, by looking at your image it seems like that data
is a nullable variable, and therefore accessing it throws a compile-time error because of sound null safety.
You have two options:
- Riskier and inelegant hasty option: just add a bang operator
!
to perform a null check, in which you're telling Dart that you're sure thatdata
isn't null - Robust, elegant, well-thought option: learn and use the null-aware operators such as
?.
,??
and??=
.
For example in the following snippet (by looking at your code):
itemCount: snapshot.data.length
I'd do something like:
itemCount: snapshot.data?.length ?? 0
Which solution might not be correct - it depends on your business logic, i.e. what you want to do.
The inelegant option would be something like
if (data != null)
itemCount: snapshot.data!.length
Which can lead to unexpected runtime errors in the long run.