0

''' I tried to connect to Mongodb database with flutter app and it's throwing certificate error and i also tried to add MyHttpOverrides but nothing fixed. the error is => "HandshakeException (HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(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;
  }
}

''' import '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");
  }
}

'''

  • Does this answer your question? [how to solve flutter CERTIFICATE\_VERIFY\_FAILED error while performing a POST request?](https://stackoverflow.com/questions/54285172/how-to-solve-flutter-certificate-verify-failed-error-while-performing-a-post-req) – Moaz El-sawaf Jun 09 '22 at 19:03
  • No nothing new! – user15317517 Jun 09 '22 at 19:12

1 Answers1

0

I had the exact same error !

It was in fact my firewall which was the culprit : seems to be a known issue (not flutter related though) : https://github.com/dart-lang/sdk/issues/32131

I turned it off just for 5min for testing and... connection worked :)

Hope it helps !

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 15:54