skip to Main Content
import 'package:flutter/material.dart';

class Something {
  Something({required this.first, required this.second});
  final String first;
  final String second;
}

class MapGood extends StatefulWidget {
  const MapGood({Key? key})
      : location = **const** Something(  // the constructor being called isnt' a const constructor. Try to remove 'const' from the invocation.
          first: "37.422",
          second: "-12",
        ),
        super(key: key);

  final Something? location;

  @override
  State<MapGood> createState() => _MapGoodState();
}

class _MapGoodState extends State<MapGood> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

In const constructor about class Something() in StatefulWidget MapGood is in error, if remove ‘const’ go in error for obvious reason.
Need to create a Stateful like that, where that input can be null, for that reason provide a default values.
I cannot understand how write this condition! i tried all passibility that i know but, every time dart pad (or vsCode) give me some errors.
That solution was give me by Codeium (or AI) and there is this error.

2

Answers


  1. There are too many solutions

    First one :

    class Something {
      Something({required this.first, required this.second});
      final String first;
      final String second;
    }
    
    final class MapGood extends StatefulWidget {
       // Remove const 
       MapGood({super.key})
          : location = Something(
              first: '37.422',
              second: '-12',
            );
    
      final Something? location;
    
      @override
      State<MapGood> createState() => _MapGoodState();
    }
    
    class _MapGoodState extends State<MapGood> {
      @override
      Widget build(BuildContext context) {
        return const Placeholder();
      }
    }
    

    Other :

    class Something {
      const Something({this.first = '37.422', this.second = '-12'});
      final String first;
      final String second;
    }
    
    final class MapGood extends StatefulWidget {
      const MapGood({super.key, this.location = const Something()});
    
      final Something? location;
    
      @override
      State<MapGood> createState() => _MapGoodState();
    }
    
    class _MapGoodState extends State<MapGood> {
      @override
      Widget build(BuildContext context) {
        return const Placeholder();
      }
    }
    
    Login or Signup to reply.
  2. Either add const to the Something constructor, like

    const Something({required this.first, required this.second});
    

    or remove const from both in front of Something and MapGood like

    class MapGood extends StatefulWidget {
      MapGood({Key? key})
          : location =  Something( 
      first: "37.422",
      second: "-12",
      ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search