如何使用Flutter WebView获取HTML元数据

时间:2019-04-23 03:27:00

标签: webview flutter

我需要使用flutter_webview从html头获取元数据。 在这种情况下,我需要从中获取价值

<!-- Chrome, Firefox OS and Opera -->
<meta name="theme-color" content="#4285f4">

所以我可以在我的应用程序上使用网站主题颜色。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果要从网页获取元数据标签,则可以使用“ alice:^ 0.0.4”库。

首先在pubspec.yaml的“ alice:^ 0.0.4”中声明

下面是我为您创建的完整示例,您可以在其中看到打印出的正文,bodyBytes,标头以及contentLength,我们可以从中获取所有“”标签:

import 'package:flutter/material.dart';
import 'package:alice/alice.dart';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

Alice alice = Alice(showNotification: true);

void main() {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Alice alice;
  Dio dio;
  HttpClient httpClient;

  @override
  void initState() {
    alice = Alice(showNotification: true);
    dio = Dio();
    dio.interceptors.add(alice.getDioInterceptor());
    httpClient = HttpClient();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: alice.getNavigatorKey(),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Alice HTTP Inspector example'),
        ),
        body: Center(
            child:
            Column(mainAxisAlignment: MainAxisAlignment.center, children: [
              RaisedButton(
                child: Text("Run HTTP Requests"),
                onPressed: _runHttpRequests,
              ),
              RaisedButton(
                child: Text("Run HTTP Insepctor"),
                onPressed: _runHttpInspector,
              ),
            ])),
      ),
    );
  }

  void _runHttpRequests() async {
    Map<String, dynamic> body = {"title": "foo", "body": "bar", "userId": "1"};
    http
        .post('https://www.google.com', body: body)
        .then((response) {
      alice.onHttpResponse(response, body: body);

      print(response.body);
      print(response.bodyBytes);
      print(response.headers);
      print(response.contentLength);

    });

    dio.post("https://www.google.com", data: body);

  }

  void _runHttpInspector() {
    alice.showInspector();
  }
}