MylocationCountry 值如何显示在 Text() 应用栏中?

How can MylocationCountry values appear in the Text() appbar?

提问人:UnityBoy 提问时间:5/10/2023 更新时间:5/10/2023 访问量:28

问:

我想在应用栏文本中显示 MyLocationCountry 的值,但数据为 null。同时,当我打印($MyLocationCountry)时,数据就在那里。我想尝试在我的应用栏上显示国家/地区位置数据,但我很困惑为什么 Mylocationcountry 上的值数据没有以文本形式显示

这是我的代码

`
Class _HomePageState extends State<HomePage> {
  int currentIndexPage = 0;

  final location = new Location();
  String? locationError;
  String? MylocationCountry, MylocationCity;
  PrayerTimes? prayerTimes;
  double? latitude, longitude, pLat, pLong;

  @override
  void initState() {

    getLocationData().then((locationData) async {
      if (!mounted) {
        return;
      }
      if (locationData != null)  {

        setState(() {
          prayerTimes = PrayerTimes(
              Coordinates(locationData.latitude, locationData.longitude),
              DateComponents.from(DateTime.now()),
              CalculationMethod.karachi.getParameters());
          pLat = prayerTimes!.coordinates.latitude;
          pLong = prayerTimes!.coordinates.longitude;
          print("Latitude : $pLat, Longitude : $pLong");
        });
      } else {
        setState(() {
          locationError = "Couldn't Get Your Location!";
        });
      }
      List<MyLocation.Placemark> placemarks = await MyLocation.placemarkFromCoordinates(pLat!, pLong!);
      MylocationCountry = placemarks.reversed.last.country.toString();
      MylocationCity = placemarks.reversed.last.locality.toString();
      print("Country : $MylocationCountry, City : $MylocationCity");
    });

    //DateTime.now().hour >= 18 && DateTime.now().hour <= 19

    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    final country = this.MylocationCountry.toString();
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(225),
        child: AppBar(
          automaticallyImplyLeading: false,
          title: RichText(
            text: TextSpan(
                text: country,
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.w500,
                ),
                children:
                <TextSpan>[
                  TextSpan(
                    text: "\n$MylocationCity",
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.normal,
                    ),
                  ),
                ]),),

`

当我像这样编写代码时,数据没有显示,也许有人可以帮助我?

Android Flutter 飞镖

评论


答:

0赞 Sushmoy 5/10/2023 #1

如果仔细查看代码,就会发现在设置 的值 后没有调用。setStateMylocationCountry

下面是更新的代码片段:

setState(() {
          List<MyLocation.Placemark> placemarks =
              await MyLocation.placemarkFromCoordinates(pLat!, pLong!);
          MylocationCountry = placemarks.reversed.last.country.toString();
          MylocationCity = placemarks.reversed.last.locality.toString();
        });
0赞 Dewa Prabawa 5/10/2023 #2

我建议你使用一种可能会有所帮助的 futureBuilder 方法:

class _HomePageState extends State<HomePage> {
  ...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(225),
        child: FutureBuilder(
          future: MyLocation.placemarkFromCoordinates(pLat!, pLong!),
          builder: (BuildContext context, AsyncSnapshot<List<MyLocation.Placemark>> snapshot) {
            if (snapshot.hasData) {
              MylocationCountry = snapshot.data!.reversed.last.country.toString();
              MylocationCity = snapshot.data!.reversed.last.locality.toString();
            }
            return AppBar(
              automaticallyImplyLeading: false,
              title: RichText(
                text: TextSpan(
                  text: MylocationCountry ?? '',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w500,
                  ),
                  children: <TextSpan>[
                    TextSpan(
                      text: "\n${MylocationCity ?? ''}",
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.normal,
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
      ...
    );
  }
}




 
0赞 Muhammad Qasim 5/10/2023 #3

MyLocation.placemarkFromCoordinates是异步的,因此它会延迟执行,这意味着您的构建函数会在地标数据可用之前运行。需要在此调用完成后更新状态。

setState((){
  MylocationCountry = placemarks.reversed.last.country.toString();
  MylocationCity = placemarks.reversed.last.locality.toString();
})