【问题标题】:Flutter TextField and The constructor being called isn't a const constructorFlutter TextField 和被调用的构造函数不是 const 构造函数
【发布时间】:2021-11-10 21:30:38
【问题描述】:

我是 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?")

一切正常。

【问题讨论】:

    标签: flutter constructor constants textfield


    【解决方案1】:

    我找到了问题的根源。 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 解决了问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多