Issue
I want to connect my android device instead of AVD with database in android studio. I am using MongoDB Database in that project.
The following code I am using to connect the database and the code works fine in AVD :
import 'package:flutter/material.dart';
import 'package:mongo_dart/mongo_dart.dart';
import 'package:mongo_dart/mongo_dart.dart' as dart_mongo;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
var Username;
void DataColl() async {
Db db = new Db("mongodb://10.0.2.2:27017/people");
await db.open();
print('connected to database');
DbCollection coll = db.collection("students");
await coll.insert({"name": Username});
var student = await coll.find().toList();
print(student);
await db.close();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("login form")),
body: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
onChanged: (text){
Username = text;
},
)
),
RaisedButton(
color: Colors.deepOrange,
elevation: 1.0,
textColor: Colors.blue,
child: Text("Submit"),
onPressed: DataColl,
)
],
),
),
);
}
}
But now I want to use my device instead of AVD to run the same code, what can I do for that?
Solution
You'll need to use LAN wifi ip address
use this command:
C:\Program Files\MongoDB\Server\4.2\bin>mongod --bind_ip ip
where ip is your LAN-Adaptor ip,
then mention this ip in your code, and connect your android device to wifi LAN, then try
Answered By - Niteesh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.