最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - An error occurs when using native tableView in Flutter - Stack Overflow

programmeradmin0浏览0评论

I am developing a function in Flutter that displays data in list format.

There is no problem if you use ListView, but you have to move the index according to a specific button, but as the number of scrollbar operations in Flutter increases, it becomes quite slow. However, in native, you can move directly even if the index is large. So, I am trying to bring native UI using UIkitView and AndroidView.

I am attaching the code to help you understand. First, we pass 20 List as parameters from flutter to the test.

return SizedBox(
      width: size.width,
      height: (widget.isSearchVisible?.value ?? false)  ?
      ((widget.bIsShowSwitch ?? false) ? size.height * 0.66 : size.height * 0.74) :
      ((widget.bIsShowSwitch ?? false) ? size.height * 0.72 : size.height * 0.81),
      child: Platform.isIOS ?
      UiKitView(
        viewType: "native_table_view",
        creationParams: <String, dynamic>{
          'data': List.generate(20, (index) => 'Item $index'),
          'cellHeight': size.height * 0.2
        },
        creationParamsCodec: StandardMessageCodec(),
      ) :
      AndroidView(
        viewType: native_key,
      ),
    );

Now, this is the code that receives data from Flutter with Swift and creates a tableView.

NativeTablePlugin.swift

import Flutter
import UIKit

public class NativeTalbeViewPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let factory = NativeTableViewFactory()
        registrar.register(factory, withId: "native_table_view") 
    }
}

public class NativeTableViewFactory: NSObject, FlutterPlatformViewFactory {
    public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
        return NativeTableViewController(frame: frame, viewId: viewId, args: args)
    }
}

NativeViewController.swift

public class NativeTableViewController: NSObject, FlutterPlatformView {
    var frame: CGRect
    var viewId: Int64
    var tableView: UITableView
    var data: [String] = []
    var cellHeight: CGFloat = 44.0 // 기본 셀 높이 설정

    init(frame: CGRect, viewId: Int64, args: Any?) {
        self.frame = frame
        self.viewId = viewId
        self.tableView = UITableView(frame: frame)
        super.init()
        self.setupTableView()

        // arguments에서 데이터와 셀 높이 받기
        if let args = args as? [String: Any] {
            if let data = args["data"] as? [String] {
                self.data = data
                self.tableView.reloadData()
            }
            if let height = args["cellHeight"] as? CGFloat {
                self.cellHeight = height
            }
        }
    }

    private func setupTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.rowHeight = cellHeight // 셀 높이를 설정
    }

    public func view() -> UIView {
        return tableView
    }
}

extension NativeTableViewController: UITableViewDataSource, UITableViewDelegate {
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count // 전달된 데이터의 개수만큼 셀 생성
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = data[indexPath.row] // 전달된 데이터로 셀에 텍스트 설정
        return cell
    }
}

Erro message

[VERBOSE-2:dart_vm_initializer(41)] Unhandled Exception: PlatformException(unregistered_view_type, A UIKitView widget is trying to create a PlatformView with an unregistered type: < native_table_view >, If you are the author of the PlatformView, make sure `registerViewFactory` is invoked.
See: /development/platform-integration/platform-views#on-the-platform-side-1 for more details.
If you are not the author of the PlatformView, make sure to call `GeneratedPluginRegistrant.register`., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310)
<asynchronous suspension>
#2      PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:242)
<asynchronous suspension>
#3      _UiKitViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:662)
<asynchronous suspension>

I connected normally and the key value is the same, but I don't know why this error occurs. Thanks for your help

发布评论

评论列表(0)

  1. 暂无评论