[Flutter] How to pass Youtube API query string from another class?



> import 'package:flutter/material.dart';
import 'package:youtube_api/youtube_api.dart';

class Utube extends StatefulWidget {
  @override
  _UtubeState createState() => _UtubeState();
}

class _UtubeState extends State<Utube> {
  static String key = "********";
  YoutubeAPI ytApi = YoutubeAPI(key);
  List<YT_API> ytResult = [];

  callAPI() async {
    String query = 'keyword';    <------------------------- pass keyword string from another class
    ytResult = await ytApi.search(query);
    ytResult = await ytApi.nextPage();
    setState(() {});
  }

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

  @override
  Widget build(BuildContext context) {
    final String keyword = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text(keyword),
      ),
      body: Container(
        child: ListView.builder(
          itemCount: ytResult.length,
          itemBuilder: (_, int index) => listItem(index),
        ),
      ),
    );
  }

  Widget listItem(index) {
    return Card(
      child: Container(
        margin: EdgeInsets.symmetric(vertical: 1.0),
        padding: EdgeInsets.all(5.0),
        child: Row(
          children: <Widget>[
            Image.network(
              ytResult[index].thumbnail['default']['url'],
            ),
            Padding(padding: EdgeInsets.only(right: 10.0)),
            Expanded(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                  Text(
                    ytResult[index].title,
                    softWrap: true,
                    style: TextStyle(fontSize: 12.0),
                  ),
                  Padding(padding: EdgeInsets.only(bottom: 1.0)),
                  Text(
                    ytResult[index].channelTitle,
                    softWrap: true,
                  ),
                ]))
          ],
        ),
      ),
    );
  }
}
1 Like