首页 > 解决方案 > Flutter TextField 和被调用的构造函数不是 const 构造函数

问题描述

我是 Flutter 的新手,正在尝试为 iOS 制作一个简单的程序。该程序将显示带有标记的谷歌地图。我需要添加搜索字符串来过滤标记。但是当我尝试将 TextField 添加到我的应用程序时 - 它因错误“被调用的构造函数不是 const 构造函数”而崩溃。


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

上面没有声明任何const。而且我在这里或通过互联网找不到任何想法,这里有什么问题。如果我要改变:

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

经过

Text("What is you looking for?")

一切正常。

标签: flutterconstructorconstantstextfield

解决方案


我找到了问题的根源。Flutter自动生成的代码(我开始新项目时): 文件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());
                               ^^^^^

删除这个const可以解决问题!


推荐阅读