Error when using addChild twice in one function in NativeScript Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Error when using addChild twice in one function in NativeScript without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to dynamically create stackLayouts and add them to the existing scrollView but got an error:

JS ERROR TypeError: scrollView.addChild is not a function. (In 'scrollView.addChild(stackLayout)', 'scrollView.addChild' is undefined)

but by using console.log it sees both of them:

StackLayout(10) ScrollView<dates>@diary/diary-page.xml:4:7;

My code is:

input.forEach(function(entry) {
    const scrollView = page.getViewById("dates");
    var stackLayout = new StackLayout();
    stackLayout.className = 'date';
    var label = new Label();
    label.text = entry.Date;
    stackLayout.addChild(label);
    console.log(stackLayout + ' ' + scrollView);
    scrollView.addChild(stackLayout);
});

Can anybody help with this strange behaviour? Thank you

Answer

Found the reason here: How to nest elements in dynamically created ScrollView?

“ScrollView is actually an implantation of ContentView so the right way to set it’s content is by doing:

scrollview.content = stacklayout

Note: Only can only set a single content view. That’s the reason there is not addChild method as it suggests multiple children are supported.”

We are here to answer your question about Error when using addChild twice in one function in NativeScript - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji