错误说只能在DART中的初始化程序中访问静态成员

时间:2019-10-03 05:29:48

标签: flutter dart

当我尝试将GenerateReportView类中存在的ID添加到_GenerateReportViewState类中定义的映射中时,出现错误,请帮助我解决此问题,请在映射中的“ ID”键中添加当前值在父类的ID中。

class GenerateReportView extends StatefulWidget {
      final String reportType;
      final Map jsonString;
      final String reportName;
      final String dateType;

  final String id;
  final String language;

  GenerateReportView(
      {@required this.id,
      @required this.language,
      @required this.jsonString,
      @required this.reportType,
      @required this.reportName})
      : dateType = jsonString['dateType'];

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

class _GenerateReportViewState extends State<GenerateReportView>
    with SingleTickerProviderStateMixin {
  TabController _TabController;
  String currentReportChartType;
  List<TextEditingController> controllers = [];
  TextEditingController controller;
  Future report;
  Map<String, dynamic> reqFormat = {"lang": "EN","id":widget.id};//here I am getting error saying "Only static members can be accessed in initializers."

2 个答案:

答案 0 :(得分:1)

您可以在initState()中初始化
代码段

class _GenerateReportViewState extends State<GenerateReportView> with SingleTickerProviderStateMixin {
  TabController _TabController;
  String currentReportChartType;
  List<TextEditingController> controllers = [];
  TextEditingController controller;
  Future report;
  Map<String, dynamic> reqFormat;

  @override
  void initState() {
    reqFormat = {"lang": "EN","id":widget.id};
    super.initState();
  }

答案 1 :(得分:0)

在承包商中插入ID并将其传递给

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class GenerateReportView extends StatefulWidget {
  final String reportType;
  final Map jsonString;
  final String reportName;
  final String dateType;

  final String id;
  final String language;

  GenerateReportView(this.id, this.language, this.jsonString, this.reportType, this.reportName)
      : dateType = jsonString['dateType'];

  @override
  _GenerateReportViewState createState() => _GenerateReportViewState(id);
}

class _GenerateReportViewState extends State<GenerateReportView>
    with SingleTickerProviderStateMixin {
  TabController _TabController;
  String currentReportChartType;
  List<TextEditingController> controllers = [];
  TextEditingController controller;
  Future report;
  final String id;

  Map<String, dynamic> reqFormat;

  _GenerateReportViewState(this.id) {
    reqFormat = {"lang": "EN", "id": id};
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return null;
  } //here I am getting error saying "Only static members can be accessed in initializers."

}

并且您不需要@required,只需移除卷曲的括号