Ошибка Метод '[]' был вызван с нулевым значением. Получатель: null Попытка вызвать: [] (0tm2JqPY0oNq5vSM74BqOufhGao1)

Я получаю эту ошибку на странице сообщения с опросом после добавления к нему функции голосования:

Я добавил новый метод handleTotalVotePosts для обработки количества userPostPolls (в строке номер 178). Я думаю, что ошибка связана с получением идентификатора пользователя или чего-то еще, в чем я не уверен.

Я не уверен, что здесь на самом деле не так

The method '[]' was called on null.
Receiver: null
Tried calling: []("0tm2JqPY0oNq5vSM74BqOufhGao1")

Но код кажется правильным

This my **pollPost.dart**
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:justpoll/libmodels/user_model.dart';
import 'package:justpoll/screens/chat/chat_data.dart';
import 'package:justpoll/widgets/progress.dart';
import 'package:justpoll/widgets/toast_text.dart';
import '../Constants.dart';
import 'package:justpoll/widgets/custom_image.dart';
import 'package:animated_button/animated_button.dart';

class PollPost extends StatefulWidget {
  final String pollPostId;
  final String mediaUrl;
  final String ownerId;
  final String username;
  final String caption;
  final String category;
  final String colorTheme;
  final Timestamp duration;
  final String question;
  final dynamic votesTotal;
  final String optText1;
  final String optText2;
  final String optText3;
  final String optText4;
  final String optemo1;
  final String optemo2;
  final String optemo3;
  final String optemo4;
  final String optCount1;
  final String optCount2;
  final String optCount3;
  final String optCount4;

  PollPost({
    this.pollPostId,
    this.mediaUrl,
    this.ownerId,
    this.username,
    this.caption,
    this.category,
    this.colorTheme,
    this.duration,
    this.question,
    this.votesTotal,
    this.optText1,
    this.optText2,
    this.optText3,
    this.optText4,
    this.optemo1,
    this.optemo2,
    this.optemo3,
    this.optemo4,
    this.optCount1,
    this.optCount2,
    this.optCount3,
    this.optCount4,
  });

  factory PollPost.fromDocument(DocumentSnapshot doc) {
    return PollPost(
      pollPostId: doc['pollPostid'],
      mediaUrl: doc['mediaUrl'],
      ownerId: doc['ownerId'],
      username: doc['username'],
      caption: doc['caption'],
      category: doc['category'],
      colorTheme: doc['colorTheme'],
      duration: doc['Duration'],
      question: doc['Question'],
      votesTotal: doc['votesTotal'],
      optText1: doc["options"]["1"]["optionText1"],
      optText2: doc["options"]["2"]["optionText2"],
      optText3: doc["options"]["3"]["optionText3"],
      optText4: doc["options"]["4"]["optionText4"],
      optemo1: doc["options"]["1"]["optionEmoji1"],
      optemo2: doc["options"]["2"]["optionEmoji2"],
      optemo3: doc["options"]["3"]["optionEmoji3"],
      optemo4: doc["options"]["4"]["optionEmoji4"],
      // optCount1: doc["options"]["1"]["optionCount1"],
      // optCount2: doc["options"]["2"]["optionCount2"],
      // optCount3: doc["options"]["3"]["optionCount3"],
      // optCount4: doc["options"]["4"]["optionCount4"],
    );
  }

  int getreactionsTotalCount(votesTotal) {
    if (votesTotal == null) {
      return 0;
    }
    int count = 0;
    votesTotal.values.forEach((val) {
      if (val = true) {
        count += 1;
      }
    });
    return count;
  }

  @override
  _PollPostState createState() => _PollPostState(
        pollPostId: this.pollPostId,
        mediaUrl: this.mediaUrl,
        ownerId: this.ownerId,
        username: this.username,
        caption: this.caption,
        category: this.category,
        colorTheme: this.colorTheme,
        duration: this.duration,
        question: this.question,
        optText1: this.optText1,
        optText2: this.optText2,
        optText3: this.optText3,
        optText4: this.optText4,
        optemo1: this.optemo1,
        optemo2: this.optemo2,
        optemo3: this.optemo3,
        optemo4: this.optemo4,
        votesTotalCount: getreactionsTotalCount(this.votesTotal),
      );
}

class _PollPostState extends State<PollPost> {
  GlobalKey floatingKey = LabeledGlobalKey("Floating");
  bool isFloatingOpen = false;
  OverlayEntry floating;
  static FirebaseAuth auth = FirebaseAuth.instance;

  final userRef = FirebaseFirestore.instance.collection("users");
  final pollPostsRef = FirebaseFirestore.instance.collection("pollPosts");
  final String currentUserId = auth.currentUser?.uid;
  final String pollPostId;
  final String mediaUrl;
  final String ownerId;
  final String username;
  final String caption;
  final String category;
  final String colorTheme;
  final Timestamp duration;
  final String question;
  final String optText1;
  final String optText2;
  final String optText3;
  final String optText4;

  final String optemo1;
  final String optemo2;
  final String optemo3;
  final String optemo4;
  int votesTotalCount;
  Map votesTotal;
  bool isVoted;

  _PollPostState({
    this.pollPostId,
    this.mediaUrl,
    this.ownerId,
    this.username,
    this.caption,
    this.category,
    this.colorTheme,
    this.duration,
    this.question,
    this.votesTotal,
    this.votesTotalCount,
    this.optText1,
    this.optText2,
    this.optText3,
    this.optText4,
    this.optemo1,
    this.optemo2,
    this.optemo3,
    this.optemo4,
  });

  handleTotalVotePosts() {
    bool _isVoted = votesTotal[currentUserId] == true;

    if (_isVoted) {
      pollPostsRef
          .doc(ownerId)
          .collection('usersPollPosts')
          .doc(pollPostId)
          .update({'votesTotal.$currentUserId': false});

      setState(() {
        votesTotalCount -= 1;
        isVoted = false;
        votesTotal[currentUserId] = false;
      });
    } else if (!_isVoted) {
      pollPostsRef
          .doc(ownerId)
          .collection('usersPollPosts')
          .doc(pollPostId)
          .update({'votesTotal.$currentUserId': true});

      setState(() {
        votesTotalCount += 1;
        isVoted = true;
        votesTotal[currentUserId] = true;
      });
    }
  }

  OverlayEntry createFloating() {
    RenderBox renderBox = floatingKey.currentContext.findRenderObject();
    Offset offset = renderBox.localToGlobal(Offset.zero);
    return OverlayEntry(builder: (context) {
      return Positioned(
        left: offset.dx,
        top: offset.dy - 70,
        child: Material(
          color: Colors.transparent,
          elevation: 20,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(40.0),
            child: Container(
              padding: EdgeInsets.all(5.0),
              height: 50,
              color: MyColors.black,
              child: Row(
                children: [
                  Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: AnimatedButton(
                          onPressed: () {
                            handleTotalVotePosts();
                            toastMessage("You voted for $optText1 $optemo1");
                            setState(() {
                              if (isFloatingOpen)
                                floating.remove();
                              else {
                                floating = createFloating();
                                Overlay.of(context).insert(floating);
                              }
                              isFloatingOpen = !isFloatingOpen;
                            });
                          },
                          duration: 20,
                          enabled: true,
                          width: 30,
                          height: 30,
                          color: MyColors.black,
                          shadowDegree: ShadowDegree.light,
                          child:
                              Text(optemo1, style: TextStyle(fontSize: 20.0)))),
                  Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: AnimatedButton(
                          onPressed: () {
                            toastMessage("You voted for $optText2 $optemo2");
                            setState(() {
                              handleTotalVotePosts();
                              if (isFloatingOpen)
                                floating.remove();
                              else {
                                floating = createFloating();
                                Overlay.of(context).insert(floating);
                              }
                              isFloatingOpen = !isFloatingOpen;
                            });
                          },
                          duration: 20,
                          enabled: true,
                          width: 30,
                          height: 30,
                          color: MyColors.black,
                          shadowDegree: ShadowDegree.light,
                          child:
                              Text(optemo2, style: TextStyle(fontSize: 20.0)))),
                  optText3.isEmpty
                      ? Text("")
                      : Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: AnimatedButton(
                              onPressed: () {
                                handleTotalVotePosts();
                                toastMessage(
                                    "You voted for $optText3 $optemo3");
                                setState(() {
                                  if (isFloatingOpen)
                                    floating.remove();
                                  else {
                                    floating = createFloating();
                                    Overlay.of(context).insert(floating);
                                  }
                                  isFloatingOpen = !isFloatingOpen;
                                });
                              },
                              duration: 20,
                              enabled: true,
                              width: 30,
                              height: 30,
                              color: MyColors.black,
                              shadowDegree: ShadowDegree.light,
                              child: Text(optemo3,
                                  style: TextStyle(fontSize: 20.0)))),
                  optText4.isEmpty
                      ? Text("")
                      : Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: AnimatedButton(
                              onPressed: () {
                                handleTotalVotePosts();
                                toastMessage(
                                    "You voted for $optText4 $optemo4");
                                setState(() {
                                  if (isFloatingOpen)
                                    floating.remove();
                                  else {
                                    floating = createFloating();
                                    Overlay.of(context).insert(floating);
                                  }
                                  isFloatingOpen = !isFloatingOpen;
                                });
                              },
                              duration: 20,
                              enabled: true,
                              width: 30,
                              height: 30,
                              color: MyColors.black,
                              shadowDegree: ShadowDegree.light,
                              child: Text(optemo4,
                                  style: TextStyle(fontSize: 20.0)))),
                ],
              ),
            ),
          ),
        ),
      );
    });
  }

  buildPollPostHeader() {
    return FutureBuilder(
      future: userRef.doc(ownerId).get(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return circularProgress();
        }

        UserModel user = UserModel.fromMap(snapshot.data.data());
        return ListTile(
          leading: CircleAvatar(
            backgroundImage: CachedNetworkImageProvider(user.photoUrl),
            backgroundColor: Colors.grey,
          ),
          title: GestureDetector(
            onTap: () => toastMessage("showing profile"),
            child: Text(
              user.username,
              style: TextStyle(
                color: MyColors.black,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          subtitle: Text(category),
          trailing: IconButton(
            onPressed: () => toastMessage("pop up menu"),
            icon: Icon(Icons.more_vert),
          ),
        );
      },
    );
  }

  buildPollPostCenter() {
    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            question,
            style: TextType.boldHeading,
          ),
        ),
        mediaUrl == null
            ? Center(
                child: Container(
                height: 30.0,
              ))
            : ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: chachedNetworkImage(
                  mediaUrl,
                ),
              ),
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 10.0),
          child: Column(
            children: [
              Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        optText1,
                        style: TextStyle(
                          color: MyColors.offBlack,
                          fontSize: 16.0,
                        ),
                      ),
                      Text(optemo1),
                      SizedBox(
                        width: 25.0,
                      ),
                      Text(
                        optText2,
                        style: TextStyle(
                          color: MyColors.offBlack,
                          fontSize: 16.0,
                        ),
                      ),
                      Text(optemo2),
                    ],
                  ),
                ],
              ),
              SizedBox(height: 13.0),
              Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        optText3,
                        style: TextStyle(
                          color: MyColors.offBlack,
                          fontSize: 16.0,
                        ),
                      ),
                      Text(optemo3),
                      SizedBox(
                        width: 25.0,
                      ),
                      Text(
                        optText4,
                        style: TextStyle(
                          color: MyColors.offBlack,
                          fontSize: 16.0,
                        ),
                      ),
                      Text(optemo4),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  buildPollPostFooter() {
    return Column(
      children: [
        Align(
          alignment: Alignment.bottomLeft,
          child: Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Padding(padding: EdgeInsets.only(top: 40.0, left: 20.0)),
                isVoted
                    ? GestureDetector(
                        key: floatingKey,
                        onTap: () {
                          setState(() {
                            if (isFloatingOpen)
                              floating.remove();
                            else {
                              floating = createFloating();
                              Overlay.of(context).insert(floating);
                            }
                            isFloatingOpen = !isFloatingOpen;
                          });
                        },
                        child: Icon(
                          Icons.poll,
                          color: MyColors.offBlack,
                          size: 28.0,
                        ),
                      )
                    : Icon(Icons.check_circle),
                Padding(padding: EdgeInsets.only(right: 20.0)),
                GestureDetector(
                  onTap: () => toastMessage("show comments"),
                  child: Icon(
                    Icons.chat,
                    color: MyColors.offBlack,
                    size: 28.0,
                  ),
                ),
                Padding(padding: EdgeInsets.only(right: 20.0)),
                GestureDetector(
                  onTap: () => toastMessage("saved successfully"),
                  child: Icon(
                    Icons.bookmark,
                    color: MyColors.offBlack,
                    size: 28.0,
                  ),
                ),
                Padding(padding: EdgeInsets.only(right: 20.0)),
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      GestureDetector(
                        onTap: () => toastMessage("sharing with friends"),
                        child: Align(
                          alignment: Alignment.topRight,
                          child: Icon(
                            Icons.send,
                            color: MyColors.offBlack,
                            size: 28.0,
                          ),
                        ),
                      ),
                      Padding(padding: EdgeInsets.only(right: 25.0)),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        Row(
          children: [
            Container(
              margin: EdgeInsets.only(left: 20.0, top: 5.0),
              child: Text(
                "$votesTotalCount votes",
                style: TextStyle(
                  color: MyColors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 20.0, top: 5.0),
              child: Text(
                "expiring in 4 days",
                style: TextStyle(
                  color: MyColors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            )
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(top: 5.0, bottom: 10.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 20.0),
                child: Text(
                  "$username ",
                  style: TextStyle(
                    color: MyColors.black,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Expanded(
                child: Text(caption),
              ),
            ],
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    isVoted = (votesTotal[currentUserId] == true);
    return Card(
      elevation: 1.0,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          buildPollPostHeader(),
          buildPollPostCenter(),
          buildPollPostFooter(),
        ],
      ),
    );
  }
}

Пожалуйста, помогите мне избавиться от этой ошибки. Спасибо :)


person Ramith K S    schedule 02.03.2021    source источник
comment
Можете указать, какой код, потому что я не могу найти строку с номером 178, спасибо.   -  person abbas jafary    schedule 02.03.2021
comment
в методе handleTotalVotePosts   -  person Ramith K S    schedule 02.03.2021


Ответы (1)


Напишите приведенный ниже код внутри вашего handleTotalVotePosts метода:

if(votesTotal == null){
  return;
}
person abbas jafary    schedule 02.03.2021
comment
да, попробовал. не работает - person Ramith K S; 03.03.2021