Hackarp
Hackarp

Reputation: 61

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

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), ^^^^

Upvotes: 4

Views: 16053

Answers (2)

Wisnu Saputra
Wisnu Saputra

Reputation: 101

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);
            },
          ),
        );
      }
  }

Upvotes: 0

Hemanth Raj
Hemanth Raj

Reputation: 34769

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!

Upvotes: 4

Related Questions