[英]print statement is not working in a function
如果我在 function lat()
中使用print
,它不会打印在控制台上。
有什么办法可以防止 function 出现这种行为,因为我无法调试应用程序?
我是 Dart/Flutter 的新手:我实际上是在尝试获取lat
和经度long
以便将它们发送到 API 并获取天气数据。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:geolocator/geolocator.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
_Home createState() => _Home();
}
class _Home extends State<Home> {
int weather = 4;
dynamic _text;
dynamic permisson;
void lat() async {
var pos = await getCurrentPosition(desiredAccuracy: LocationAccuracy.high).then((position) {
dynamic loc = position.toJson;
print(loc);
setState(() { _text = position.toJson(); });
});
}
@override
void initState() {
super.initState();
lat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image(
image: NetworkImage('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
height: 250
),
Center(
child: ListTile(
title: Text(weather.toString()),
subtitle: Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TextButton(
child: const Text('BUY TICKETS'),
onPressed: () {
setState(() { weather = 4; });
},
),
const SizedBox(width: 8),
TextButton(
child: const Text('LISTEN'),
onPressed: () {
print(_text['longitude']);
},
),
const SizedBox(width: 8),
],
),
],
),
),
],
),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Color(0xFF6200EE),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white.withOpacity(.60),
selectedFontSize: 14,
unselectedFontSize: 14,
onTap: (value) {
print(value);
if(value == 0) {
print("home");
Navigator.pushNamed(context, '/home');
} else if (value == 1) {
print("Settings");
} else if (value == 2) {
print("Profile");
} else if (value == 3) {
print("About");
}
},
items: [
BottomNavigationBarItem(
title: Text('Home'),
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
title: Text('Setting'),
icon: Icon(Icons.settings),
),
BottomNavigationBarItem(
title: Text('Profile'),
icon: Icon(Icons.account_circle),
),
BottomNavigationBarItem(
title: Text('About'),
icon: Icon(Icons.info),
),
],
),
);
}
}
要检查您在应用程序中的位置,或者如果您的打印件没有工作或检查天气,您的值是否为 null 。 你应该总是用你的印刷品来连接一些东西,比如:
print("My Longitude: ${_text['longitude']}");
在打印中,如果您有一个变量,如myVar
、 myData
或myName
,那么您可以在打印中正常使用它,例如:
print(myVar);
print(myData);
print(myName);
但是如果您想要从_text['longitude']
、 office.employee
、 value.data.name
等变量中获取值。 您应该始终像这样使用Print()
:
print(${value.data.data});
print(${office.employee});
print(_text['longitude']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.