颤振Firebase应用中的getter键被调用为null

时间:2020-04-21 13:10:13

标签: flutter

,伙计们,我需要您的帮助,我正在按照一个Udemy教程“使用Reed的Flutter构建社交网络应用程序”,但是我有一个小问题。当我运行我的应用程序时,它一直告诉我getter键是在null上调用的。我已经与这个错误作斗争了两天。请帮忙。下面是我的代码。

控制台错误:

   (27685): 0
D/GraphicBuffer(27685): register, handle(0x97a46580) (w:480 h:960 s:480 f:0x1 u:f02)
D/GraphicBuffer(27685): register, handle(0x95465580) (w:480 h:960 s:480 f:0x1 u:f02)
D/GraphicBuffer(27685): register, handle(0x954657c0) (w:480 h:888 s:480 f:0x1 u:f02)
I/flutter (27685): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (27685): The following NoSuchMethodError was thrown during performLayout():
I/flutter (27685): The getter 'key' was called on null.
I/flutter (27685): Receiver: null
I/flutter (27685): Tried calling: key
I/flutter (27685):
I/flutter (27685): The relevant error-causing widget was:
I/flutter (27685):   ListView file:///C:/Users/DELL/Documents/fluttershare/fluttershare/lib/pages/profile.dart:284:12
I/flutter (27685):
I/flutter (27685): When the exception was thrown, this was the stack:
I/flutter (27685): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter (27685): #1      SliverChildListDelegate.build (package:flutter/src/widgets/sliver.dart:676:27)

主页上的个人资料页面类

Profile(profileId:currentUser?.id),

个人资料页面本身

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:fluttershare/models/user.dart';
import 'package:fluttershare/pages/edit_profile.dart';
import 'package:fluttershare/pages/home.dart';
import 'package:fluttershare/widgets/header.dart';
import 'package:fluttershare/widgets/post.dart';
import 'package:fluttershare/widgets/post_tile.dart';
import 'package:fluttershare/widgets/progress.dart';

class Profile extends StatefulWidget {

  final String profileId;

   Profile({this.profileId});



  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
   String postOrientation ="grid";
   bool isloading=false;
  final String currentUserid=currentUser?.id;

   int postCount=0;
   List<Post>posts=[];

  @override
  void initState() { 

    super.initState();
    getProfilePost();

  }

   getProfilePost()async{

   setState(() {

      isloading=true;
   });

    QuerySnapshot snapshot=await postRef.document( widget.profileId)
   .collection("userPosts")
   .orderBy("timestamp",descending: true).getDocuments();
   setState(() {
     isloading=false;
     postCount=snapshot.documents.length;
     posts= snapshot.documents.map((doc)=>Post.fromDocument(doc)).toList();
   });
    }

   Column buildCountColumn(String label, int count){

     return Column(
       mainAxisSize: MainAxisSize.min,
       mainAxisAlignment: MainAxisAlignment.center,
       children: <Widget>[
         Text(
           count.toString(),
           style:  TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold),
           ),
           Container(
             margin:EdgeInsets.only(top: 4.0),
             child: Text(
               label,
               style: TextStyle(
                 color:  Colors.grey,
                 fontSize: 15.0,
                 fontWeight: FontWeight.w400
               ),
             ),
           )
       ],
     );

  }


  editProfile(){
    Navigator.push(context, MaterialPageRoute(builder: (context)=>
    EditProfile(currentUserId:currentUserid)));
  }


    Container  buildButton({String text, Function function}){
   return Container(
      padding: EdgeInsets.only(top: 2.0),
      child: FlatButton(
        onPressed: function,
         child: Container(
           width: 250.0,
           height: 27.0,
           alignment: Alignment.center,
           child: Text(text, style: TextStyle(color: Colors.white, 
           fontWeight: FontWeight.bold),),
           decoration: BoxDecoration(
             color: Colors.blue,
             border: Border.all(
               color:  Colors.blue,
             ),
             borderRadius: BorderRadius.circular(5.0),

           ),
         )),
 );

  }

  buildProfileButton(){

    //for viewing your own profile-show edit profile button

      bool isProfileOwner=currentUserid==widget.profileId;
      if(isProfileOwner){

        return buildButton(
          text: "Edit Profile",
          function: editProfile,
        );
      }
  }
buildProfileHeader(){

 FutureBuilder(
   future: userRef.document(widget.profileId).get(),
   builder: (context, snapshot){

     if(!snapshot.hasData){
       return circularProgress();
     }

      User user= User.fromDocument(snapshot.data);
      return Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            Row( 
              children: <Widget>[
                CircleAvatar(
                  radius: 40.0,
                  backgroundColor: Colors.grey,
                  backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                ),
                 Expanded(
                   flex: 1,
                   child: Column(
                     children: <Widget>[
                       Row(mainAxisSize: MainAxisSize.max,
                       mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                       children: <Widget>[
                         buildCountColumn("posts",postCount),
                         buildCountColumn("followers",0),
                         buildCountColumn("following",0),
                       ],
                       ),
                       Row(
                         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                         children: <Widget>[
                           buildProfileButton(),
                         ],
                       )
                     ],
                   )
                   )
              ],
            ),
            Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(top: 12.0),
              child: Text(user.username, style: TextStyle(
                color: Colors.grey, fontWeight: FontWeight.bold, fontSize: 15.0),
                ),
            ),
             Container(
               alignment: Alignment.centerLeft,
               padding: EdgeInsets.only(top: 4.0),
               child: Text(user.displayName, style: TextStyle(
                color: Colors.grey, fontWeight: FontWeight.bold,),
                ),
             ),
             Container(
                alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(top: 2.0),
               child: Text(user.bio,style: TextStyle(fontStyle: FontStyle.normal), 
                ),
             )
          ],
        ),
        );
   });
}

   setPostOrientation(String postOrientation){

     this.postOrientation=postOrientation;
   }
  buildProfilePost(){

     if(isloading){
       return circularProgress();
     }
     else if(posts.isEmpty){

       return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SvgPicture.asset(
            "assets/images/no_content.svg",
            height: 260.0,
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.0),
              child: Text(
                "No Posts",
                style: TextStyle(color: Colors.redAccent, fontSize: 40.0, fontWeight: FontWeight.bold),
              ),

          )
        ],
      ),
    );

     }
     else if(postOrientation=="grid"){


     List<GridTile>gridTiles=[];

     posts.forEach((post){

     gridTiles.add(GridTile(child:PostTile(post)));
     });

     return GridView.count(
       crossAxisCount: 3,
       childAspectRatio: 1,
       mainAxisSpacing: 1.5,
        crossAxisSpacing: 1.5,
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        children: gridTiles,
       );
  } else if(postOrientation=="list"){
               return Column(
       children:posts ,
     );



  }


     }
  buildTogglePostOrientation(){
    return Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
     children: <Widget>[
       IconButton(
         onPressed: ()=>setPostOrientation("grid"),
         icon: Icon(Icons.grid_on, 
         color:  postOrientation=="grid"
         ?Theme.of(context).primaryColor:Colors.grey,) 
         ),
          IconButton(
         icon: Icon(Icons.list, 
         color:  postOrientation=="list"
         ?Theme.of(context).primaryColor:Colors.grey,), 
         onPressed: ()=>setPostOrientation("list"),)
     ],
    );

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: header(context,titleText: "Profile"),
      body:ListView(
        children: <Widget>[
          buildProfileHeader(),
          Divider(),
          buildTogglePostOrientation(),
           Divider(
             height: 0.0,
           ),
           buildProfilePost(),
        ],
      )
    );
  }
}

错误指向此处的列表视图

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: header(context,titleText: "Profile"),
      body:ListView(
        children: <Widget>[
          buildProfileHeader(),
          Divider(),
          buildTogglePostOrientation(),
           Divider(
             height: 0.0,
           ),
           buildProfilePost(),
        ],
      )
    );
  }
}

请帮助

0 个答案:

没有答案