0
Good afternoon!
I’m trying to learn how to use the Dio
to integrate an API that I am also developing and for this I tried to carry out a simple test.
I simply tried to make a print on the terminal even using this code snippet:
class _MyHomePageState extends State<MyHomePage> {
void teste() async {
Response res;
Dio dio = new Dio();
res = await dio.get("192.168.15.10:3000/welcome");
print(res.data.toString());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Verifique o teste no terminal!!!:',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: teste,
child: Icon(Icons.adjust),
),
);
}
}
The route (Express) I’m using on Dio.get():
router.get('/', (req, res) => {
res.send({ message: 'Parabéns você está conectado a API' })
})
But when I run test in the app by pressing the button, I get the following error (As it is too big, I decided to upload a screenshot of it, aiming not to pollute the post too much):
What did I do wrong? Thanks in advance.
I never used this Dio, I already accessed the API in Flutter using a dependency called http, I always use res.body. Try trading the res.data for the res..
– Rafael Costa
Another thing, I think you should use a widget called Futurebuilder to work with asynchronous functions. I’m gonna put some codes in here that I made that might help you
– Rafael Costa