我正在使用laravel后端api开发一些Flutter项目。在我的登录页面上,我想从我的laravel api中提取用户,但它总是返回上面的错误。这是我拍打的api文件:
class CallApi {
final String _url = 'http://192.168.10.17/';
postData(data, apiUrl) async {
var fullUrl = _url + apiUrl + await _getToken();
return http.post(
fullUrl,
body: jsonDecode(data) as _InternalLinkedHashMap<String, String>,
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
}
);
}
getData(apiUrl) async {
var fullUrl = _url + apiUrl;
return await http.get(
fullUrl,
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
}
);
}
_getToken() async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
var token = localStorage.getString('token');
return '?token=$token';
}
}
这是我的后端api:
Route::post('/login', 'User@company');
这是我在登录页面中对api的实现:
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:orbital_app/Api/orbitalApi.dart';
import 'package:orbital_app/main.dart';
class Login extends StatefulWidget{
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool _isLoading = false;
_showMsg(msg){
final snackBar = SnackBar(
content: Text(msg),
action: SnackBarAction(
label: 'Close',
onPressed: () {}
),
);
Scaffold.of(context).showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.only(top: 100),
child: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 70),
child: Column(
children: <Widget>[
Text('Katsana Logo'),
Text('Katsana', style: TextStyle(
fontFamily: 'Roboto',
fontSize: 40
)),
Text('Orbital', style: TextStyle(
fontFamily: 'Roboto',
fontSize: 30
))
]
)
),
Container(
padding: EdgeInsets.all(10),
child: TextField(
controller: emailController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Email'
)
)
),
Container(
padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
child: TextField(
obscureText: true,
controller: passwordController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
)
)
),
Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
const Radius.circular(50)
)
),
padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
child: RaisedButton(
textColor: Colors.white,
color: Color.fromRGBO(42,215,157,1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
child: Text(_isLoading ? 'Loging...' : 'Login'),
onPressed: _isLoading ? null : _login
)
)
]
)
)
);
}
void _login() async {
setState(() {
_isLoading = true;
});
var data = {
'email': emailController.text,
'password': passwordController.text
};
var response = await CallApi().postData(data, 'login');
var body = json.decode(response.body).cast<CallApi>();
print(body);
}
}
任何帮助将不胜感激。
答案 0 :(得分:1)
似乎您收到了 String 作为响应,但是您将其显式转换为 _InternalLinkedHashMap