简体   繁体   English

Argument 类型 'Context' 不能分配给参数类型 'BuildContext' - Flutter

[英]The Argument type 'Context' can't be assigned to the parameter type 'BuildContext' - Flutter

I'm getting an error on context, I also found a similar question here on stackoverflow but it didn't solve my problem.我在上下文中遇到错误,我也在 stackoverflow 上发现了一个类似的问题,但它没有解决我的问题。 The answer to that question suggested to add import 'package:path/path.dart';该问题的答案建议添加import 'package:path/path.dart'; but still I'm having the same error.但我仍然有同样的错误。 Here is my code below:这是我的代码如下:

import 'dart:js';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:chewie/chewie.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:fluttershare/models/user.dart';
import 'package:fluttershare/pages/comments.dart';
import 'package:fluttershare/pages/home.dart';
import 'package:fluttershare/widgets/progress.dart';
import 'package:video_player/video_player.dart';
import 'package:path/path.dart';

class Post extends StatefulWidget {
  final String postId;
  final String ownerId;
  final String username;
  final String foodname;
  final String placename;
  final String cityname;
  final String statename;
  final String mediaUrl;
  final double rating;
  final dynamic likes;

  Post({
    this.postId,
    this.ownerId,
    this.username,
    this.foodname,
    this.placename,
    this.cityname,
    this.statename,
    this.mediaUrl,
    this.rating,
    this.likes,
  });

  factory Post.fromDocument(DocumentSnapshot doc) {
    return Post(
      postId: doc['postId'],
      ownerId: doc['ownerId'],
      username: doc['username'],
      foodname: doc['foodname'],
      placename: doc['placename'],
      cityname: doc['cityname'],
      statename: doc['statename'],
      mediaUrl: doc['mediaUrl'],
      rating: doc['rating'],
      likes: doc['likes'],
    );
  }

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

  @override
  _PostState createState() => _PostState(
        postId: this.postId,
        ownerId: this.ownerId,
        username: this.username,
        foodname: this.foodname,
        placename: this.placename,
        cityname: this.cityname,
        statename: this.statename,
        mediaUrl: this.mediaUrl,
        likes: this.likes,
        likeCount: getLikeCount(this.likes),
      );
}

class _PostState extends State<Post> {
  final String currentUserId = currentUser?.id;
  final String postId;
  final String ownerId;
  final String username;
  final String foodname;
  final String placename;
  final String cityname;
  final String statename;
  final String mediaUrl;
  String rating;
  int likeCount;
  Map likes;
  bool isLiked;

  _PostState({
    this.postId,
    this.ownerId,
    this.username,
    this.foodname,
    this.placename,
    this.cityname,
    this.statename,
    this.mediaUrl,
    this.rating,
    this.likes,
    this.likeCount,
  });

  buildPostHeader() {
    return FutureBuilder(
      future: usersRef.document(ownerId).get(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return circularProgress();
        }
        User user = User.fromDocument(snapshot.data);
        return ListTile(
          leading: CircleAvatar(
            backgroundImage: CachedNetworkImageProvider(user.photoUrl),
            backgroundColor: Colors.grey,
          ),
          title: GestureDetector(
            onTap: () => print('showing profile'),
            child: Text(
              user.username,
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          trailing: IconButton(
            onPressed: () => print('deleting post'),
            icon: Icon(Icons.more_vert),
          ),
        );
      },
    );
  }

  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;

  @override
  void initState() {
    _controller = VideoPlayerController.network(mediaUrl);
    _initializeVideoPlayerFuture = _controller.initialize();
    _controller.setLooping(true);
    _controller.setVolume(1.0);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  //                                     Post Chewie Display...

  buildPostImage() {
    return FittedBox(
      fit: BoxFit.contain,
      child: mounted
          ? Chewie(
              controller: ChewieController(
                videoPlayerController: _controller,
                aspectRatio: 16 / 9,
                autoPlay: false,
                autoInitialize: true,
                looping: true,
              ),
            )
          : Container(),
    );
  }

  handleLikePost() {
    bool _isLiked = likes[currentUserId] == true;

    if (_isLiked) {
      postsRef
          .document(ownerId)
          .collection('userPosts')
          .document(postId)
          .updateData({'likes.$currentUserId': false});
      setState(() {
        likeCount -= 1;
        isLiked = false;
        likes[currentUserId] = false;
      });
    } else if (!_isLiked) {
      postsRef
          .document(ownerId)
          .collection('userPosts')
          .document(postId)
          .updateData({'likes.$currentUserId': true});
      setState(() {
        likeCount += 1;
        isLiked = true;
        likes[currentUserId] = true;
      });
    }
  }

  buildPostFooter() {
    return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Padding(padding: EdgeInsets.only(top: 40.0, left: 20.0)),
            GestureDetector(
              onTap: handleLikePost,
              child: Icon(
                isLiked ? Icons.favorite : Icons.favorite_border,
                size: 28.0,
                color: Colors.red,
              ),
            ),
            Padding(padding: EdgeInsets.only(right: 20.0)),
            GestureDetector(
              onTap: () => showComments(
                context,
                postId: postId,
                ownerId: ownerId,
                mediaUrl: mediaUrl,
              ),
              child: Icon(
                Icons.supervised_user_circle,
                size: 28.0,
                color: Colors.blueAccent,
              ),
            ),
            Padding(padding: EdgeInsets.only(right: 50.0)),
            Icon(Icons.location_on, color: Colors.blueAccent),
            Container(
              margin: EdgeInsets.only(left: 2.0, top: 5.0, right: 10.0),
              child: Text("$cityname " + "$statename",
                  style: TextStyle(color: Colors.blueAccent)),
            ),
          ],
        ),
        Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0),
              child: Text(
                "$likeCount likes",
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0, top: 10.0, bottom: 5.0),
              child: Text(
                "$foodname ",
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(padding: EdgeInsets.only(left: 20.0)),
            Icon(
              Icons.restaurant,
              color: Colors.blueAccent,
            ),
            Container(
              color: Colors.blueAccent,
              padding: EdgeInsets.all(5.0),
              margin: EdgeInsets.only(left: 2.0),
              child: Text(placename.toUpperCase(),
                  style: TextStyle(color: Colors.white)),
            ),
          ],
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    isLiked = (likes[currentUserId] == true);
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        buildPostHeader(),
        buildPostImage(),
        buildPostFooter(),
        Divider(),
        Padding(padding: EdgeInsets.only(bottom: 10.0)),
      ],
    );
  }
}

showComments(BuildContext context,
    {String postId, String ownerId, String mediaUrl}) {
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return Comments(
      postId: postId,
      postOwnerId: ownerId,
      postMediaUrl: mediaUrl,
    );
  }));
}

The argument type 'Context' error... Image here:参数类型“上下文”错误...这里的图片:

The argument type Context can't be assigned to the parameter type BuildContext .参数类型Context不能分配给参数类型BuildContext

So you need to pass the BuildContext instead of Context type.所以你需要传递BuildContext而不是Context类型。 Try passing the build context to the buildPostFooter method from widget's build method.尝试将构建上下文从小部件的构建方法传递给buildPostFooter方法。

buildPostFooter(BuildContext context){
  ...

  GestureDetector(
    onTap: () => showComments(
      context,
      postId: postId,
      ownerId: ownerId,
      mediaUrl: mediaUrl,
   ),
   child: Icon(
       Icons.supervised_user_circle,
       size: 28.0,
       color: Colors.blueAccent,
     ),
   ),

   ...
}

Then in your widget's build method.然后在您的小部件的构建方法中。 I hope this will solve your problem.我希望这能解决你的问题。

buildPostFooter(context),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法将参数类型“Context”分配给参数类型“BuildContext” - Error : The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 错误:无法将参数类型“Context”分配给参数类型“BuildContext” - error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 无法将参数类型“Context”分配给参数类型“BuildContext” - The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 参数类型“JsObject”不能分配给参数类型“BuildContext”。 - flutter - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'. - flutter Flutter Navigator“无法将参数类型&#39;Context&#39;分配给参数类型&#39;BuildContext&#39;” - Flutter Navigator “argument type 'Context' can't be assigned to the parameter type 'BuildContext'” 参数类型“BuildContext?” 不能分配给参数类型“BuildContext” - The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext' 错误:[dart] 无法将参数类型“Context”分配给参数类型“BuildContext”。 [argument_type_not_assignable] - error: [dart] The argument type 'Context' can't be assigned to the parameter type 'BuildContext'. [argument_type_not_assignable] 参数类型“上下文”不能分配给参数类型“BuildContext”。dartargument_type_not_assignable) - The argument type 'Context' can't be assigned to the parameter type 'BuildContext'.dartargument_type_not_assignable) 参数类型“Context”不能分配给参数类型“BuildContext”。 For 循环和按钮 - The argument type 'Context' can't be assigned to the parameter type 'BuildContext'. For Loop and Button 错误:无法将参数类型“JsObject”分配给参数类型“BuildContext” - Error: The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM