Flutter TextField and The constructor being called isn't a const constructor

Viewed 942

I'm newbie in flutter and trying to make a simple program for iOS. This program will show google map with markers. And I need to add search string to filter markers. But when I tried to add TextField to my app - it crushes with the error "The constructor being called isn't a const constructor".


import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'src/locations.dart' as locations;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
...
@override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: []);
    return MaterialApp(
      home: Scaffold(

        body: Stack(
          children: <Widget>[
            GoogleMap(
              onMapCreated: _onMapCreated,
              initialCameraPosition: CameraPosition(
                target: _center,
                zoom: 11.0,
              ),
              mapType: _currentMapType,
              zoomControlsEnabled: false,
              myLocationEnabled: false,
              onCameraMove: _onCameraMove,
              markers: _rmarkers.values.toSet(),
            ),
            Positioned(
              top: 32,
              left: 5,
              child: Container(
                margin: EdgeInsets.all(20),
                height: 70,
                width: 300,
                
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(15)),
                    boxShadow: <BoxShadow>[
                      BoxShadow(
                          blurRadius: 10,
                          offset: Offset(5.0, 5.0),
                          color: Colors.black.withOpacity(0.8))
                    ]),
                child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Icon(Icons.search, size: 36.0),
                      Container(
                          padding: EdgeInsets.all(3.0),
                          child: Expanded(
==>   HERE                    child: TextField(   
==>   THE                       decoration: InputDecoration(
==>   PROBLEM                     hintText: "What is you looking for?",
                            ),
                          ))),
                      Icon(Icons.account_circle, size: 36.0),
                    ]),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

There is no any const declared above. And I can't find any idea here or over internet what's wrong here. If I would change:

TextField(
 decoration: InputDecoration(
   hintText: "What is you looking for?",
 ),
)

by

Text("What is you looking for?")

Everything works fine.

1 Answers

I have found the source of the problem. The code automatically generated by flutter (when I started new project): File test/widget_test.dart

    void main() {

       testWidgets('Counter increments smoke test', (WidgetTester tester) async {
       // Build our app and trigger a frame.
       await tester.pumpWidget(const MyApp());
                               ^^^^^

Removing this const is solving the problem!

Related