What does the error “Can't access this in a field initializer mean”?

眉间皱痕 提交于 2021-01-28 22:10:28

问题


I want to create a class in flutter for displaying an Alert Box, which can take in title and Content as input to show error box. But debug console says that "can't access this in a field initializer" when I used this to access a variable of the same class in AlertDialog().

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: Alert("Say Hy","Hy"),));

class Alert extends StatelessWidget{

  final String titlea;
  final String contexta;

  Alert(this.titlea,this.contexta);

  AlertDialog dialog = AlertDialog(
    title: Text(this.titlea),
    content: Text(this.contexta),
  );



    Widget build(BuildContext context){
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: (){
              showDialog(context: context ,builder: (BuildContext context) => dialog);
            },
          ),
        );
      }
  }

Error: Can't access 'this' in a field initializer. title: Text(this.titlea), ^^^^ Error: Can't access 'this' in a field initializer. content: Text(this.contexta), ^^^^


回答1:


Yes, you are trying to access a field of a class and trying to use it as field initializer for AlertDialog class which is not possible. You can try to initialize it along with other fields or make AlertDialog a getter.

Example: Initialize with other fields

class Alert extends StatelessWidget {
  final String titlea;
  final String contexta;
  final AlertDialog dialog;

  Alert(this.titlea, this.contexta)
      : dialog = AlertDialog(
          title: Text(titlea),
          content: Text(contexta),
        );

  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
              context: context, builder: (BuildContext context) => dialog);
        },
      ),
    );
  }
}

Or: Make it as a getter

class Alert extends StatelessWidget {
  final String titlea;
  final String contexta;

  Alert(this.titlea, this.contexta);

  AlertDialog get dialog => AlertDialog(
        title: Text(titlea),
        content: Text(contexta),
      );

  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
              context: context, builder: (BuildContext context) => dialog);
        },
      ),
    );
  }
}

Hope that helps!




回答2:


Please remove this on Widget text. Because this for constructor.

class Alert extends StatelessWidget{

  final String titlea;
  final String contexta;

  Alert(this.titlea,this.contexta);

  AlertDialog dialog = AlertDialog(
    title: Text(titlea),
    content: Text(contexta),
  );



    Widget build(BuildContext context){
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: (){
              showDialog(context: context ,builder: (BuildContext context) => dialog);
            },
          ),
        );
      }
  }


来源:https://stackoverflow.com/questions/56985004/what-does-the-error-cant-access-this-in-a-field-initializer-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!