skip to Main Content

Using scrollview, I created a system that retrieves the first 10 users from the database and returns the remaining users in the scroll. The last user will be null at first, and then the value will be assigned. I think I got an error because the method is null at the first time.

UPDATE:

 @override
  void initState() {
    super.initState();
    //build metodu tetiklendikten sonra gelicek (context belli olduktan sorna)
    WidgetsBinding.instance.addPostFrameCallback((_) {

      getUser(_sonUser!); <---------------- ERROR!!
      
      print("SCHEDULERBİNDİNG ÇALIŞTI");
    });
    _scrollController.addListener((() {
      (_scrollController.offset >= _scrollController.position.maxScrollExtent &&
              !_scrollController.position.outOfRange)
          ? getUser(_sonUser!) // En Altta

          : (_scrollController.offset <=
                      _scrollController.position.minScrollExtent &&
                  !_scrollController.position.outOfRange)
              ? print("En Üstte")
              : print("scroll ediliyor");
    }));
  }

metod is

getUser(User1 sonUser) async {
    final _usermodel = Provider.of<UserModel>(context, listen: false);

    if (!_dahaVarmi) {
      print("ELEMAN KALMADI MK");
      return;
    }
    if (_yukleniyor) {
      return;
    }
    setState(() {
      _yukleniyor = true;
    });
    sonUser = _kullanicilarListesi!.last;
    List<User1> users = await _usermodel.getUsersWithPagination(
        sonUser, _getirilecekElemanCount);
    if (_kullanicilarListesi == null) {
      _kullanicilarListesi = [];
      _kullanicilarListesi!.addAll(users);
    } else {
      _kullanicilarListesi!.addAll(users);
    }
    if (users.length < _getirilecekElemanCount) {
      _dahaVarmi = false;
    }
    for (var element in querySnapshot.docs) {
      User1 tekUser = User1.fromMap(element.data() as Map<String, dynamic>);
      _kullanicilarListesi!.add(tekUser);
      debugPrint("Getirelen usr  name  ==> ${tekUser.userName}");
    }*/

    sonUser = _kullanicilarListesi!.last;
    debugPrint("en son user name ==>  ${sonUser.userName}");

    setState(() {
      _yukleniyor = false;
    });
  }

database metod

 @override
  Future<List<User1>> getUsersWithPagination(
      User1? lastgetUser, int getirilecekElemanSayisi) async {
    QuerySnapshot querySnapshot;
    List<User1> _tumKullanicilar = [];
    debugPrint("ilk kullanıcılar getriliyor");
    if (lastgetUser == null) {
      debugPrint("ilk kullanıcılar getriliyor");
      //firestore ilk 10 tane elemanı vericek ve ordby sayesinde Username i a dan z dizicek
      querySnapshot = await FirebaseFirestore.instance
          .collection("users")
          .orderBy("userName")
          .limit(getirilecekElemanSayisi)
          .get();
    } else {
      debugPrint("sonrakiler getiriliyor");
      querySnapshot = await FirebaseFirestore.instance
          .collection("users")
          .orderBy("userName")
          .startAfter([lastgetUser.userName])
          .limit(getirilecekElemanSayisi)
          .get();
      await Future.delayed(const Duration(
        seconds: 1,
      ));
    }
    for (var element in querySnapshot.docs) {
      User1 tekUser = User1.fromMap(element.data() as Map<String, dynamic>);
      _tumKullanicilar.add(tekUser);
      debugPrint("Getirelen usr  name  ==> ${tekUser.userName}");
    }

    return _tumKullanicilar;
  }

ERORR message

Exception caught by scheduler library ═════════════════════════════════
The following _CastError was thrown during a scheduler callback:
Null check operator used on a null value

When the exception was thrown, this was the stack
#0 _KullanicilarPAgeState.initState.
kullanicilar.dart:39
#1 SchedulerBinding._invokeFrameCallback
binding.dart:1175
#2 SchedulerBinding.handleDrawFrame
binding.dart:1113
#3 SchedulerBinding._handleDrawFrame
binding.dart:1015
#4 _invoke (dart:ui/hooks.dart:148:13)
#5 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:318:5)
#6 _drawFrame (dart:ui/hooks.dart:115:31)

I checked sonuser in several places, but the error was still the same.(if-else)

2

Answers


  1. You are having incorrect condition on checking null.

    WidgetsBinding.instance.addPostFrameCallback((_) {
          if (_sonUser == null) {//Before _sonUser != null
            print("object");
          } else {
            getUser(_sonUser!);
          }
          // getUser(_sonUser!);
          print("SCHEDULERBİNDİNG ÇALIŞTI");
        });
    
    Login or Signup to reply.
  2. You have logic issue in the code.

    Change

      getUser(_sonUser!); <---------------- ERROR!!
    

    to

    if(_sonUser!=null){
       getUser(_sonUser!);
    }
    

    And even change

     _scrollController.addListener((() {
          (_scrollController.offset >= _scrollController.position.maxScrollExtent &&
                  !_scrollController.position.outOfRange)
              ? getUser(_sonUser!) // En Altta
    
              : (_scrollController.offset <=
                          _scrollController.position.minScrollExtent &&
                      !_scrollController.position.outOfRange)
                  ? print("En Üstte")
                  : print("scroll ediliyor");
        }));
    

    to

     _scrollController.addListener((() {
          (_scrollController.offset >= _scrollController.position.maxScrollExtent &&
                  !_scrollController.position.outOfRange)
              ? _sonUser!=null ? getUser(_sonUser!)  // 👈 add this check as well
    
              : (_scrollController.offset <=
                          _scrollController.position.minScrollExtent &&
                      !_scrollController.position.outOfRange)
                  ? print("En Üstte")
                  : print("scroll ediliyor");
        }));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search