如何连接 android 设备而不是 AVD 与数据库

How to connect android device instead of AVD with database

我想连接我的 android 设备而不是 AVD 与 android 工作室中的数据库。我在该项目中使用 MongoDB 数据库。

我用来连接数据库的以下代码在 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,
                )
              ],
            ),
          ),
        );
      }
    }

但现在我想使用我的设备而不是 AVD 来 运行 相同的代码,我该怎么做?

您需要使用局域网wifi ip地址 使用这个命令: C:\Program Files\MongoDB\Server.2\bin>mongod --bind_ip ip 其中 ip 是您的 LAN 适配器 ip, 然后在您的代码中提及此 ip,并将您的 android 设备连接到 wifi LAN,然后尝试