flutteruser-agentflutter-http

flutter multi-platform user-agent


I'm developing a multiplatform project with flutter for Android, iOS, and web. How can I get user-agent for adding to the header of the request?

I use http package for requests. the sample user agent that I want:

Google sdk_gphone_x86, Android 11(30)

PostmanRuntime/7.26.8

Chrome, Mozilla, Safari


Solution

  • use device info plus package

    then use like below:

    import 'package:device_info_plus/device_info_plus.dart';
    
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    print('Running on ${androidInfo.model}');  // e.g. "Moto G (4)"
    
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    print('Running on ${iosInfo.utsname.machine}');  // e.g. "iPod7,1"
    
    WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
    print('Running on ${webBrowserInfo.userAgent}');  // e.g. "Mozilla/5.0 
    (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"