【问题标题】:Access this in a field initializer in flutter widget在颤振小部件的字段初始化程序中访问它
【发布时间】:2020-05-23 18:57:26
【问题描述】:

这是我的第一个扁平化应用。我尝试做的是这样的 ab 容器布局:

class _HomePageState extends State<HomePage> {
  final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

  Position _currentPosition;

    @override
    Widget build(BuildContext context) {
        return Scaffold(
                appBar: AppBar(
                        title: Text("LocInfo"),
                ),
                body:
        ListView(
          children: [
            //_getCurrentLocation(),
            locSection,
            addressSection,
            timeSection,
          ],
        ),
        );
    }

在布局里面我有这个小部件:

Widget locSection = Container(
  padding: const EdgeInsets.all(32),
  child: Row(
    children: [
      Expanded(
        /*1*/
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            /*2*/
            Container(
              padding: const EdgeInsets.only(bottom: 8),
              child: Text(
                'Location (LAT/LNG)',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            if (_currentPosition != null)
            Text(
              "LAT: ${_currentPosition.latitude}, LNG: ${_currentPosition.longitude}",
              style: TextStyle(
                color: Colors.grey[500],
              ),
            ),
          ],
        ),
      ),
      /*3*/
      Icon(
        Icons.near_me,
      ),
    ],
  ),
);

我还有一个获取坐标的功能:

_getCurrentLocation() {
    final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

    geolocator
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
            .then((Position position) {
                setState(() {
                    _currentPosition = position;
                });
      _getAddressFromLatLng();
            }).catchError((e) {
                print(e);
            });
}

问题是,我收到了这个错误:

lib/home_page.dart:52:21:错误:无法在字段中访问“this” 初始化程序读取'_currentPosition'。 if (_currentPosition != null)

那么我如何在这个位置访问_currentPosition

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    所以,你的

    Widget locSection = Container(
      padding: const EdgeInsets.all(32),...
    

    就像初始化程序,此时 _HomePageState 的实例尚未形成。所以你不能访问它的变量_currentPosition。

    您可以在 _HomePageState 初始化期间(在构造函数中)或在其初始化之后(例如在 initState 中)创建 locSection,然后它将正常工作。

    【讨论】:

    • 所以你的意思是在这部分:class HomePage extends StatefulWidget { @override _HomePageState createState() =&gt; _HomePageState(); }
    • 我真的很困惑。这是我的完整代码:gist.github.com/MalteKiefer/556b2181f6e30ad536468c005ef55104
    • 我的意思是这样的:Widget locSection; //变量_HomePageState() { //这是构造函数) locSection = Container( padding: const EdgeInsets.all(32), //你可以在这里访问_currentPosition ... }
    • 对不起,我不明白。所以你的意思是:class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); }
    • 简单来说就是在_HomePageState的构造函数中创建locSection。也就是说,在您的 _HomePageState 类中。 _HomePageState() { locSection = .... }
    猜你喜欢
    • 2022-07-15
    • 2022-09-25
    • 2021-12-25
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多