我还不了解该框架,您可以帮助解决此错误吗?我正在尝试获取基本的用户数据 enter image description here
颤抖的医生:
[!] Android工具链-为Android设备开发(Android SDK 版本30.0.0) !不接受某些Android许可证。要解决此问题,请运行: 扑医生--android-licenses [!] Android Studio(4.0版) X Flutter插件未安装;这增加了Flutter特有的 功能。 X Dart插件未安装;这增加了Dart特有的 功能。 [√]已连接的设备(1个可用)
!医生发现了2类问题。
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'constants.dart';
import 'package:simple_auth/simple_auth.dart' as simpleAuth;
import 'package:simple_auth_flutter/simple_auth_flutter.dart';
class Inst_Login extends StatefulWidget {
Inst_Login({Key key}) : super(key: key);
@override
_Inst_LoginState createState() => _Inst_LoginState();
}
class _Inst_LoginState extends State<Inst_Login> {
String _errorMsg;
Map _userData;
final simpleAuth.InstagramApi _igApi = simpleAuth.InstagramApi(
"instagram",
Constants.igClientId,
Constants.igClientSecret,
Constants.igRedirectURL,
scopes: [
'user_profile', // For getting username, account type, etc.
'user_media', // For accessing media count & data like posts, videos etc.
],
);
@override
void initState() {
super.initState();
SimpleAuthFlutter.init(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Instagram Basic Display API Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Visibility(
visible: _userData != null,
child: Text(
_userData.keys.fold(
'', (kvText, key) => kvText + "$key: ${_userData[key]} \n"),
textAlign: TextAlign.center,
),
replacement:
Text("Click the button below to get Instagram Login."),
),
FlatButton.icon(
icon: Icon(Icons.input),
label: Text(
"Get Profile Data",
),
onPressed: _loginAndGetData,
color: Colors.pink.shade400,
),
if (_errorMsg != null) Text("Error occured: $_errorMsg"),
],
),
),
);
}
Future<void> _loginAndGetData() async {
_igApi.authenticate().then(
(simpleAuth.Account _user) async {
simpleAuth.OAuthAccount user = _user;
var igUserResponse =
await Dio(BaseOptions(baseUrl: 'https://graph.instagram.com')).get(
'/me',
queryParameters: {
// Get the fields you need.
// https://developers.facebook.com/docs/instagram-basic-display-api/reference/user
"fields": "username,id,account_type,media_count",
"access_token": user.token,
},
);
setState(() {
_userData = igUserResponse.data;
_errorMsg = null;
});
},
).catchError(
(Object e) {
setState(() => _errorMsg = e.toString());
},
);
}
}
答案 0 :(得分:0)
即使您使用的是Visibility widget
,如果您正在执行async
并返回Future
然后使用FutureBuilder
的api调用,则小部件也不应具有空的_userdata。 / p>
FutureBuilder的代码将为-
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'constants.dart';
import 'package:simple_auth/simple_auth.dart' as simpleAuth;
import 'package:simple_auth_flutter/simple_auth_flutter.dart';
class Inst_Login extends StatefulWidget {
Inst_Login({Key key}) : super(key: key);
@override
_Inst_LoginState createState() => _Inst_LoginState();
}
class _Inst_LoginState extends State<Inst_Login> {
String _errorMsg;
Map _userData;
final simpleAuth.InstagramApi _igApi = simpleAuth.InstagramApi(
"instagram",
Constants.igClientId,
Constants.igClientSecret,
Constants.igRedirectURL,
scopes: [
'user_profile', // For getting username, account type, etc.
'user_media', // For accessing media count & data like posts, videos etc.
],
);
@override
void initState() {
super.initState();
SimpleAuthFlutter.init(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Instagram Basic Display API Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder(
future: _loginAndGetData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done)
return Text(
_userData.keys.fold('',
(kvText, key) => kvText + "$key: ${_userData[key]} \n"),
textAlign: TextAlign.center,
);
return Text("Click the button below to get Instagram Login.");
},
),
FlatButton.icon(
icon: Icon(Icons.input),
label: Text(
"Get Profile Data",
),
onPressed: _loginAndGetData,
color: Colors.pink.shade400,
),
if (_errorMsg != null) Text("Error occured: $_errorMsg"),
],
),
),
);
}
Future<void> _loginAndGetData() async {
_igApi.authenticate().then(
(simpleAuth.Account _user) async {
simpleAuth.OAuthAccount user = _user;
var igUserResponse =
await Dio(BaseOptions(baseUrl: 'https://graph.instagram.com')).get(
'/me',
queryParameters: {
// Get the fields you need.
// https://developers.facebook.com/docs/instagram-basic-display-api/reference/user
"fields": "username,id,account_type,media_count",
"access_token": user.token,
},
);
setState(() {
_userData = igUserResponse.data;
_errorMsg = null;
});
},
).catchError(
(Object e) {
setState(() => _errorMsg = e.toString());
},
);
}
}
希望如果您有任何问题或疑问要发表评论,这将为您提供帮助。