无法在字段初始化程序中访问“this”以读取“assists”[重复]

Posted

技术标签:

【中文标题】无法在字段初始化程序中访问“this”以读取“assists”[重复]【英文标题】:Can't access 'this' in a field initializer to read 'assists' [duplicate] 【发布时间】:2021-07-08 23:59:54 【问题描述】:

您好,我是 Flutter 的初学者,我遇到了一个以前从未遇到过的奇怪错误:无法在字段初始化程序中访问“this”以读取“assists”。 无法在字段初始值设定项中访问“this”以读取“goals”。

这是我尝试过的:

class Statss extends StatefulWidget 




  @override
  _StatssState createState() => _StatssState();


class _StatssState extends State<Statss> 

  List<AssistsJson> topassists = [];
  List<GoalsJson> topgoals = [];
  List<GoalsJson> teams = [];
  String assister;
  String assists;
  String goals;
  String scorer;


  Future<void>  getScorers() async 
    http.Response response = await http.get(
        'https://v3.football.api-sports.io/players/topscorers?league=39&season=2020',
        headers: 'x-rapidapi-key': ApiKey.key,
          'x-rapidapi-host':'v3.football.api-sports.io');
    String body = response.body;
    var data = jsonDecode(body);
    List<dynamic> clubList = data['response'];

    setState(() 
      topgoals = clubList
          .map((dynamic item) => GoalsJson.fromJson(item))
          .toList();
      goals = topassists[0].statistics[0].goals.total.toString();
    );

  

  Future<void>  getAssisters() async 
    http.Response response = await http.get(
        'https://v3.football.api-sports.io/players/topassists?league=39&season=2020',
        headers: 'x-rapidapi-key': ApiKey.key,
          'x-rapidapi-host':'v3.football.api-sports.io');
    String body = response.body;
    var data = jsonDecode(body);
    List<dynamic> clubList = data['response'];

    setState(() 
      topassists = clubList
          .map((dynamic item) => AssistsJson.fromJson(item))
          .toList();
      assists = topassists[0].statistics[0].goals.assists.toString();

    );

  

  @override
  void initState() 
    super.initState();
    getScorers();
    getAssisters();
  






  List<String> images = [
    "Assets/tt.jpg",
    "Assets/qatarairways.jpg",
    "Assets/LOGO_Danao.jpg",
    "Assets/delice.jpg"
  ];

  List<String> menu = [
    "Assets/tt.jpg",
    "Assets/qatarairways.jpg",
    "Assets/LOGO_Danao.jpg",
    "Assets/delice.jpg"
  ];

  List<String> choices = [
    "Top Scorer: ",
    "Top Assister: ",
    "Asset: ",
    "Ass: "
  ];
  List<String> mchoices = [
    "Goals: ",
    "Assists: ",
    "Goals: ",
    "Ass: "
  ];

  List<String> data = [
    goals,
    assists,
    "Goals: ",
    "Ass: "
  ];
..........................................................................

我正在尝试了解发生了什么并找到解决此问题的方法

【问题讨论】:

【参考方案1】:

将分配逻辑移至 initistate

List<String> data = [ ];

@override
  void initState() 
    super.initState();
    getScorers();
    getAssisters();
 data = [
    goals,
    assists,
    "Goals: ",
    "Ass: "
   ];
  

【讨论】:

以上是关于无法在字段初始化程序中访问“this”以读取“assists”[重复]的主要内容,如果未能解决你的问题,请参考以下文章

无法在字段初始化程序中访问此内容以读取“属性”

无法读取 Object.eval [as updateDirectives] 处未定义的属性“性别”

无法在事件处理程序中访问 React 实例(this)[重复]

as3 以普通影片剪辑的形式访问子项(gotoAndPlay 等)

无法读取未定义的属性“URL” - 访问 JSON 对象

Android:无法从CursorWindow读取第0行,第3列。在从中访问数据之前,请确保正确初始化Cursor