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

时间:2020-02-25 14:13:06

标签: flutter

我正在尝试呈现设置列表。每个设置都有一个不同的控制器。例如,控制器可以是开关。或按钮。

设置课程

import 'package:flutter/cupertino.dart';

class Setting
{
  String key;
  Widget controller;

  Setting(this.key, this.controller);
}

但是当我尝试将列表作为字段时,它说它只能是静态成员,因此我不能使用变量和功能。如何在项目生成器中直接声明此列表?也许使用.toList()的传播算子?我不太确定该怎么写。 设置页面状态

class _TheSettingsPageState extends State<TheSettingsPage> {

  bool _isMilesPerHour = globals.preferences.speedNotation.isMilesPerHour;
  bool _isKilometersPerHour = globals.preferences.speedNotation.isKilometersPerHour;
  File _pickedImage;
  List<Setting> settings = [
    Setting
    (
      SpeedNotation.milesPerHour.shortNotation,
      Switch(
        value: _isMilesPerHour, 
        onChanged: _onMilesPerHourToggled
      ),
    ),
    Setting
    (

    ),
    Setting
    (

    ),
  ];

  _onMilesPerHourToggled(bool isMilesPerHour){
    setState(() {
      _isMilesPerHour = isMilesPerHour;
      _isKilometersPerHour = !isMilesPerHour;
    });
    globals.preferences.writeSettings();
  }

  _onKilometersPerHourToggled(bool isKilometersPerHour){
    setState(() {
      _isKilometersPerHour = isKilometersPerHour;
      _isMilesPerHour = !isKilometersPerHour;
    });
    globals.preferences.writeSettings();
  }

  Future getImageAsync() async{
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _pickedImage = image;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: Padding(
          padding: EdgeInsets.all(20.0),
          child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: 
          [ 
            Expanded(
              flex: 1,
              child: Row
              (
                children: [
                  Align(
                    alignment: Alignment.topLeft,
                    child: NavigatePopButton(),
                  ),
                  Spacer(),
                  Align
                  (
                    alignment: Alignment.topCenter,
                    child: Text
                    (
                      'Settings',
                      style: Theme.of(context).textTheme.title,
                      overflow: TextOverflow.visible,
                    ),
                  ),
                  Spacer(),
                ],
              )
            ),
            Expanded(
              flex: 4,
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: ListView.separated(
                  itemCount: 2,
                  separatorBuilder: (BuildContext context, int index) => Divider(height: 1.0), 
                  itemBuilder: (BuildContext context, int index){
                      return settings[index].controller;
                  }       
                ),
              ),
            ),
          ]
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试在#include<iostream> #include<assert.h> #include<vector> #include<chrono> int iter = 10000; std::vector<double> coeffs = { 4, 2, -2, 5, 0, 15}; std::vector<double> valuesToEvaluate = {0, 2, -4, 8, 15, 1.25, 512 ,-5.3 ,12.215, 153, 23, -11}; auto start = std::chrono::high_resolution_clock::now(); do{ std::vector<double> results = Polyval(coeffs, valuesToEvaluate); } while(iter-->0); auto end = std::chrono::high_resolution_clock::now(); long duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); std::cout << duration << std::endl; 内进行所有初始化

类似这样的东西

initState()