【问题标题】:Connect MongoDb to Flutter App throws Certificate error将 MongoDb 连接到 Flutter App 会引发证书错误
【发布时间】:2022-07-02 23:35:31
【问题描述】:

''' 我尝试使用颤振应用程序连接到 Mongodb 数据库并且它正在抛出证书 错误,我也尝试添加 MyHttpOverrides 但没有修复。 错误是=>“HandshakeException(HandshakeException:客户端握手错误(操作系统错误: CERTIFICATE_VERIFY_FAILED: 无法获取本地颁发者证书(handshake.cc:393)))"

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:projectv/routes/routes.dart';
import 'package:projectv/screens/home.dart';
import 'package:projectv/screens/property_detail.dart';
import 'package:projectv/utitlity/mongo_db.dart';
import './screens/agent.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HttpOverrides.global = MyHttpOverrides();
  await MongoDbConnection.dbConnect();
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarColor: Color.fromARGB(255, 83, 167, 236), // status bar color
    ),
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Zulu real estate',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const Home(),
        routes: {
          Routes.propertyDetails: (context) => PropertyDetail(),
          Routes.propertyAgent: (context) => AgentScreen(),
        });
  }
}

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

''' 导入'package:mongo_dart/mongo_dart.dart';

class MongoDbConnection {
  static var dbUrl, dbCollection;
  static dbConnect() async {
    dbUrl = await Db.create(
        "mongodb+srv://abdi:abdi@cluster0.soti0.mongodb.net/myFirstDatabase?retryWrites=true&w=majority");
    await dbUrl.open(secure: true);
    dbCollection = dbUrl.collection("agents");
  }
}

'''

【问题讨论】:

标签: flutter


【解决方案1】:

我在尝试使用我的颤振应用程序连接到 mongo db 时遇到同样的问题。 我尝试使用建议的方法 Hereenter link description here 但它对我没有帮助。有什么解决办法吗?

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2021-03-26
    • 2020-08-25
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多