dlink_httpdns 3.0.0
dlink_httpdns: ^3.0.0 copied to clipboard
HttpDNS Flutter Plugin
example/lib/main.dart
import 'package:dlink_httpdns/dns_info.dart';
import 'package:dlink_httpdns/dns_info_callback.dart';
import 'package:dlink_httpdns/httpdns_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:dlink_httpdns/dlink_httpdns.dart';
class MyDNSInfoCallback extends DNSInfoCallback {
@override
void onSuccess(DNSInfo info) {
if (kDebugMode) {
print("getDNSInfoAsync success info:$info");
}
}
@override
void onFailed(String message) {
if (kDebugMode) {
print("getDNSInfoAsync failed message:$message");
}
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
createDNS(['HOST1','HOST2']);
}
Future<void> createDNS(List<String> hosts) async {
DLinkHttpDNS dns = DLinkHttpDNS();
HttpDNSConfig config = HttpDNSConfig('ACCOUNT_ID', enableLog: kDebugMode);
/// Please call the create method before using DLinkHttpDNS.
await dns.create(config);
/// Set the domain name that needs to be pre-resolved.
dns.setPreResolveHosts(hosts);
Future.delayed(const Duration(seconds: 4), () async {
/// This method only queries the cache and returns the resolution result in the cache, which may return an empty result.
/// If there is no resolution result in the cache or the resolution result in the cache has expired,
/// the domain name resolution will be performed in the worker thread.
/// After the resolution is successful, the cache will be updated for the next call to domain name resolution.
DNSInfo? info = await dns.getDNSInfoFromCache(hosts[0]);
if (kDebugMode) {
print('getDNSInfoFromCache info:$info');
}
});
/// This method first queries the cache.
/// If there is an available resolution result in the cache, the resolution result is immediately returned through the callback.
/// If there is no available resolution result in the cache, the domain name resolution will be performed in the worker thread,
/// and the resolution result will be returned through the callback after the domain name resolution is completed or the timeout period is reached.
dns.getDNSInfoAsync(hosts[0], callback: MyDNSInfoCallback());
/// This method first queries the cache.
/// If there is an available resolution result in the cache, the resolution result is returned immediately.
/// If there is no available resolution result in the cache, the thread currently calling the resolution will be blocked and the domain name resolution will be performed in the thread.
/// The resolution result will be returned after the domain name resolution is completed, or an empty result will be returned after the timeout period is reached.
DNSInfo? info = await dns.getDNSInfoSync(hosts[0]);
if (kDebugMode) {
print('getDNSInfoSync info:$info');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
),
);
}
}