将列表索引数据传递给有状态小部件以动态加载 PDF 文档

Passing List index Data to a Stateful Widget to load PDF document dynamically

我正在尝试将列表的 'index' 传递给有状态小部件。但它不起作用,因为 'index' 未被识别。

我从哪里传递数据:

onTap: () {
  Navigator.push(context,
    MaterialPageRoute(builder: (context) => DetailsPage(index)));
},

数据传递位置:

    class DetailsPage extends StatefulWidget {
       final int index;
       DetailsPage(this.index);
    
      @override
      _MyDetailsPage createState() => _MyDetailsPage();
    }
    
    class _MyDetailsPage extends State<DetailsPage> {
      bool _isLoading = true;
      PDFDocument document;
    
      @override
      void initState() {
        super.initState();
        loadDocument();
      }
    
      List locations = [
        Books(
            name: 'book name 1',
            Booklink:
            'https://storage.googleapis.com/school_books/Class9/ban/Bangla%20Sahitto.pdf'),
        Books(
            name: 'book name 2',
            Booklink:
            'https://storage.googleapis.com/school_books/Class9/ban/Bangla%20Sahitto.pdf')
      ];
    
      loadDocument() async {
        document = await PDFDocument.fromURL(locations[index].Booklink);
        setState(() => _isLoading = false);
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(),
            body: Center(
              child: _isLoading
                  ? Center(child: CircularProgressIndicator())
                  : PDFViewer(
                      document: document,
                      zoomSteps: 1,
                    ),
            ),
          ),
        );
      }
    }

知道我做错了什么吗?

因为您从另一个页面传递数据以使用该对象或变量,所以您需要使用小部件然后调用您的对象或变量,如下所示:

locations[widget.index]