Flutter食谱提取插件recipe_extractor的使用

发布于 1周前 作者 htzhanglong 来自 Flutter

Flutter食谱提取插件recipe_extractor的使用

获取开始

你可以通过以下链接安装recipe_extractor插件:

使用方法

recipe_extractor插件可以从烹饪网站的URL中提取食谱信息。如果某个元素无法被提取,则其值将为null

RecipeData 类结构

项目 类型 值描述
name String? 食谱名称
image String? 展示图像链接
servings String? 份量(例如:供3人食用,供10个松饼等)
ingredients List<String>? 成分列表
instructions List<String>? 准备说明列表
source String? 食谱来源URL

示例代码

import 'package:recipe_extractor/recipe_extractor.dart';

void main() async {
  // 指定要提取食谱的URL
  const String recipeUrl =
      "https://www.allrecipes.com/recipe/218792/ggs-chocolate-sheet-cake";

  // 提取食谱数据
  RecipeData recipeData = await extractRecipe(recipeUrl);

  // 打印食谱信息
  print(recipeData.name); // 输出食谱名称
  print(recipeData.image); // 输出展示图像链接
  print(recipeData.servings); // 输出份量
  print(recipeData.ingredients?.join("\n")); // 输出成分列表
  print(recipeData.instructions?.join("\n")); // 输出准备说明列表
  print(recipeData.source); // 输出食谱来源URL
}

更多关于Flutter食谱提取插件recipe_extractor的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter食谱提取插件recipe_extractor的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,关于Flutter中recipe_extractor插件的使用,以下是一个基本的代码案例,展示了如何集成和使用这个插件来提取食谱信息(假设recipe_extractor插件已经存在并具有相应功能)。请注意,实际使用时需要根据插件的具体API文档进行调整。

首先,确保在pubspec.yaml文件中添加recipe_extractor依赖项(假设该插件已经发布到pub.dev):

dependencies:
  flutter:
    sdk: flutter
  recipe_extractor: ^x.y.z  # 替换为实际版本号

然后,运行flutter pub get来安装依赖。

接下来,在Flutter项目中创建一个页面或服务来使用recipe_extractor插件。以下是一个简单的示例:

import 'package:flutter/material.dart';
import 'package:recipe_extractor/recipe_extractor.dart'; // 假设插件包名为recipe_extractor

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Recipe Extractor Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: RecipeExtractorDemo(),
    );
  }
}

class RecipeExtractorDemo extends StatefulWidget {
  @override
  _RecipeExtractorDemoState createState() => _RecipeExtractorDemoState();
}

class _RecipeExtractorDemoState extends State<RecipeExtractorDemo> {
  String _recipeText = """
  # Chocolate Cake

  ## Ingredients
  - 2 cups flour
  - 2 cups sugar
  - 3/4 cup unsweetened cocoa powder
  - 1 1/2 teaspoons baking soda
  - 1 1/2 teaspoons baking powder
  - 1 teaspoon salt
  - 2 large eggs
  - 1 cup whole milk
  - 1/2 cup vegetable oil
  - 2 teaspoons vanilla extract
  - 1 cup boiling water

  ## Instructions
  1. Preheat oven to 350°F (175°C). Grease and flour two 9 inch round baking pans.
  2. In a large bowl, stir together the flour, sugar, cocoa, baking soda, baking powder, and salt.
  3. Add the eggs, milk, oil, and vanilla. Beat on medium speed for 2 minutes.
  4. Stir in the boiling water. The batter will be thin. Pour batter into prepared pans.
  5. Bake for 30 to 35 minutes, or until a toothpick inserted into the center comes out clean.
  """;

  Recipe? _extractedRecipe;

  @override
  void initState() {
    super.initState();
    _extractRecipe();
  }

  Future<void> _extractRecipe() async {
    try {
      _extractedRecipe = await RecipeExtractor.extractFromText(_recipeText);
      setState(() {});
    } catch (e) {
      print("Error extracting recipe: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Recipe Extractor Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (_extractedRecipe != null)
              Text(
                'Recipe Title: ${_extractedRecipe!.title}',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            SizedBox(height: 16),
            if (_extractedRecipe != null)
              Text(
                'Ingredients:\n${_extractedRecipe!.ingredients.join("\n")}',
                style: TextStyle(fontSize: 16),
              ),
            SizedBox(height: 16),
            if (_extractedRecipe != null)
              Text(
                'Instructions:\n${_extractedRecipe!.instructions.join("\n")}',
                style: TextStyle(fontSize: 16),
              ),
            SizedBox(height: 16),
            if (_extractedRecipe == null)
              CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}

// 假设Recipe类定义如下(根据插件的实际API调整)
class Recipe {
  final String title;
  final List<String> ingredients;
  final List<String> instructions;

  Recipe({required this.title, required this.ingredients, required this.instructions});
}

// 注意:这里假设RecipeExtractor.extractFromText是一个静态方法,返回一个Future<Recipe?>
// 实际使用时,请根据插件的API文档进行调整
extension RecipeExtractor on RecipeExtractorClass {
  static Future<Recipe?> extractFromText(String text) async {
    // 这里应该是插件提供的实际提取逻辑
    // 例如:return await RecipeExtractorClass().extract(text);
    // 由于我们没有实际的插件代码,这里只是一个占位符
    return null; // 需要替换为实际的提取逻辑
  }
}

注意

  1. 上面的代码示例中包含了一个假设的Recipe类和一个扩展方法RecipeExtractor,这是因为实际的recipe_extractor插件API和类结构未知。你需要根据插件的实际文档替换这些部分。
  2. RecipeExtractorClassextractFromText方法的实现需要替换为插件提供的实际API调用。
  3. 如果插件需要初始化或配置,请在调用提取方法之前进行适当的设置。

确保查阅recipe_extractor插件的官方文档以获取准确的API信息和用法示例。

回到顶部