error: Non-nullable instance field 'searchSnapshot' must be initialized

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';


class DiscoverScreen extends StatefulWidget {
  @override
  _DiscoverScreenState createState() => _DiscoverScreenState();
}

class _DiscoverScreenState extends State<DiscoverScreen> {

  DatabaseMethods databaseMethods = new DatabaseMethods();
  TextEditingController searchUserController = new TextEditingController();
  QuerySnapshot searchSnapshot;

  bool isLoading = false;
  bool haveUserSearched = false;

I'm getting a use as late error, but when I use it as late, I get this error -> LateInitializationError: Field 'searchSnapshot' has not been initialized. Does anyone know how to solve this?

Upvotes: 0

Views: 40

Answers (1)

Ahmad hassan
Ahmad hassan

Reputation: 1009

If you don't have value of variable while initializing it you must have to make it a nullable by using ? operator.

QuerySnapshot? searchSnapshot;

if you still want to make it non-nullable just assign a value while declaring or use late keyword in this case you have to assign a value to this variable in initState.

late QuerySnapshot searchSnapshot;

Upvotes: 1

Related Questions